how to have "required" fields in my html form
Posted: Mon Mar 16, 2009 4:53 pm
Hi guys, I have my form:
and this is the action page:
Is there anyway to display an error message if the projectname field was left empty? because right now even if the field is empty it gets added to the database.
thanks
Code: Select all
<form id="form2" method="post" action="do_addjob.php">
<div id="table"><table width="200" border="0">
<tr>
<td>Project Role Name</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><label>
<input type="text" name="pname1" id="pname1" compulsory="yes"/>
</label></td>
<td><label>
<input type="submit" name="submit" id="submit" value="Proceed" />
</label></td>
<td><label></label></td>
</tr>
</table>
Code: Select all
$projectname = $_POST['pname1'];
if (isset($_REQUEST['submit'])) {
# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
$query = "INSERT INTO conmg.Job_ID (Job_ID, Job_Name) VALUES (NULL, '$projectname')";
if($result = mysql_query($query)) {
echo "$projectname has been added, please fill in the details below:";
} else {
echo "ERROR: ".mysql_error();
}
}
thanks