Page 1 of 1

how to have "required" fields in my html form

Posted: Mon Mar 16, 2009 4:53 pm
by nishmgopal
Hi guys, I have my form:

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>&nbsp;</td>
    <td>&nbsp;</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>
 
and this is the action page:

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();
}
}  
 
 
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

Re: how to have "required" fields in my html form

Posted: Tue Mar 17, 2009 7:14 am
by susrisha
put a validation through javascript on the client side.

Re: how to have "required" fields in my html form

Posted: Tue Mar 17, 2009 7:40 am
by jayshields
Validation on the client side is only a good idea if you're going to double check it on the server side.

You're better off just using server side validation and halfing the work (unless you're an exceptional case and want to minimise page requests). Just use something like

Code: Select all

empty()
on the $_POST variable (be careful with integers and empty() though). Also, don't pass $_POST variables straight into SQL queries. Use

Code: Select all

mysql_real_escape_string()

Re: how to have "required" fields in my html form

Posted: Tue Mar 17, 2009 7:57 am
by kendopt
You can make it in php

make some if conditions on the do_addjob.php page:

Code: Select all

 
 
if (isset($projectname))
 
{
 mysql_query $query;
echo "$projectname has been added, please fill in the details below:";
}
 
 else if (!$projectname)
 
{
   echo "Project name was not inserted!";
}
 
 else
 
{
  echo "ERROR: ".mysql_error();
}
 

Re: how to have "required" fields in my html form

Posted: Tue Mar 17, 2009 8:37 am
by nishmgopal
thanks for your help guys.

Re: how to have "required" fields in my html form

Posted: Tue Mar 17, 2009 9:02 am
by kendopt
is it working?

Re: how to have "required" fields in my html form

Posted: Tue Mar 17, 2009 9:04 am
by nishmgopal
yep...i used the javascript at first, but now i using the php method

Re: how to have "required" fields in my html form

Posted: Tue Mar 17, 2009 9:39 am
by kendopt
my opinion is that you mix the less languages you need. else it would be a big mess