Client side validation

I have a "Student Notes" PHP/MySql application that I would like to have all fields not be blank when the student submits the form. I am not sure where or how to ensure this. I use three files to do the work - display, edit, update where/how would I insert the validation in this code.. Thanks!!!!!

display.php

<?php
require ("config.php");
$db = mysql_connect("$hostname", "$username", "$password");
mysql_select_db("$database");
$sql = "SELECT * FROM $table WHERE id=$id";
$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);
?>

<TABLE BORDER="0" cellpadding="2" width="50"><TR><TD VALIGN="TOP" width="4"><BR>
<? include("leftnav.php"); ?>
</TD>

<TD valign="top" width="0">
<p style="margin-right:10;"><br></TD>
<TD valign="top" align="center" width="650">

<!-- CONTENT -->

<BR>

<TABLE BORDER="0" WIDTH="650" BGCOLOR="#FFFFFF" CELLPADDING="10"><TR><TD VALIGN="TOP" width="650">
<p align="left"><BR>
<CENTER><span class="title">Title <?php echo $myrow["title"] ?></SPAN></CENTER><BR>
<CENTER><span class="title">Instructor - <?php echo $myrow["instructor"] ?><br>Subject - <?php echo $myrow["subject"] ?><br>Author - <?php echo $myrow["author"] ?><br></SPAN></CENTER>
<p align="center">~~~~~~~~~~<br></p>
<PRE>
</pre>
<span> <table width=100% class="notes"><?php echo $myrow ["notes"] ?> </SPAN><BR><BR>

</PRE>
</TD></TR></TABLE>

<BR>

<!-- /CONTENT -->

                    </SPAN> <table width=100% align=center class=title>
                        <td>
                            <p align="center"><A HREF="edit.php?id=<? echo $id; ?>">Edit This Note</A>&nbsp;- &nbsp; <A HREF="deleteconf.php?id=<? echo $id; ?>">Delete This Note</A> 
        &nbsp;-&nbsp;&nbsp;<a href="pfv.php">Printer-friendly version</a>

</TD></TR><A HREF="edit.php?id=<? echo $id; ?>"></TABLE>

<span><span><? include("footer.php"); ?></span></SPAN>
</td>
</tr>
</table>
</td>
</tr>
</table>


edit.php

<? include("header.php"); ?>

<? $today = date("F j, Y g:i a"); ?>

<?php

require ("config.php");
$db = mysql_connect("$hostname", "$username", "$password");
mysql_select_db("$database");
$sql = "SELECT * FROM $table WHERE id=$id";
$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);
?>

<TABLE BORDER="0" cellpadding="5"><TR><TD VALIGN="TOP"><BR>
<? include("leftnav.php"); ?>
</TD>
<TD valign="top">&nbsp;&nbsp;</TD>
<TD valign="top" align="center" width="650">

<FORM ACTION="update.php" METHOD="POST" >
<input TYPE="hidden" VALUE="<? echo $id; ?>" NAME="id">
<span class="title">Note Title</SPAN><BR>
<input type=text size="82" value="<?php echo $myrow["title"] ?>" name="title"><BR><BR>
<span class="title">Instructor:</SPAN> <input type=text size="82" pattern="[A-Za-z]+" errorMsg="Invalid charcter found in firstname" value="<?php echo $myrow["instructor"] ?>" name="instructor"><BR>
<span class="title">Subject:&nbsp;&nbsp;&nbsp;&nbsp;</SPAN> <input type=text size="82" value="<?php echo $myrow["subject"] ?>" name="subject"><BR>
<span class="title">Author:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><input type=text size="82" value="<?php echo $myrow["author"] ?>" name="author"><BR>
<span class="title">Writing Area</SPAN><BR>
<textarea name="notes" cols="75" rows=10 wrap="PHYSICAL"><?php echo $myrow["notes"] ?></textarea >
<BR>

<input type=submit value="Save Note" name="submit" CLASS="title" ><BR>

</form>
</TD></TR></TABLE>


update.php

<?php

require ("config.php");
$db = mysql_connect("$hostname", "$username", "$password");
mysql_select_db("$database");
mysql_query("UPDATE $table SET title='$title', instructor='$instructor', subject='$subject', author='$author', notes='$notes' WHERE id='$id'");
mysql_close();
header("Location: display.php?id=$id");
?>

<p>&nbsp;</p>

    you can't do clientside validation with php as it's a server side language you would have to use Javascript or something similar.
    You could validate the data with PHP in update.php

    <?
    $err="";
    if(empty($_POST['title'])
    {
      $err.="Title not filled in<br>\n";
    }
    if(empty($_POST['instructor'])
    {
      $err.="Instructor not filled in<br>\n";
    }
    
    if($err!="")
    {
      header("Location: edit.php?id=".$id."&err=".$err);
    }
    ?>

    HTH
    Rob

      Rob - thanks for the quick response. I have tried the code you supplied and something similar, but I have not been able to make it work. Mybe I just don't understand where to place the code in my update.php file. I have tried it as the first thing in the code and just before the update with no luck...

      Thanks!!!

        Write a Reply...