how to have "required" fields in my html form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

how to have "required" fields in my html form

Post 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
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

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

Post by susrisha »

put a validation through javascript on the client side.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

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

Post 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()
kendopt
Forum Newbie
Posts: 7
Joined: Mon Mar 16, 2009 8:08 pm

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

Post 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();
}
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

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

Post by nishmgopal »

thanks for your help guys.
kendopt
Forum Newbie
Posts: 7
Joined: Mon Mar 16, 2009 8:08 pm

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

Post by kendopt »

is it working?
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

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

Post by nishmgopal »

yep...i used the javascript at first, but now i using the php method
kendopt
Forum Newbie
Posts: 7
Joined: Mon Mar 16, 2009 8:08 pm

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

Post by kendopt »

my opinion is that you mix the less languages you need. else it would be a big mess
Post Reply