Jump to content

PHP Form Validation


Davecachia

Recommended Posts

Hey all.

 

I've been posting here for a couple days.. some of you will already be familiar with my project.

I'm at the data validation stage of the project.

 

I am a strong C++ programmer so building the if statements and logic operations should not be difficult (it will just come down to PHP syntax errors).

 

Where should I be doing validation? ON the HTML end or the PHP end?

 

Right now my HTML form is complete. My PHP file is fed values, and the values are read into variables.

 

If there is something wrong with their values, I want them to be able to finish what they've started (so that they don't have to re-fill the form if something was wrong.

 

ALSO:

Some questions being answered can have an impact on other questions.. so say you answered NO to question 1. Questions 2-5 might not be available. What is the best way to take care of this?

Link to comment
Share on other sites

I would say from the php validation point of view I agree with the post above about having it in the same file as the HTML but above it. This is a common method.

 

However, with regards to your second question for question impacts. I would take a look at javascript validation and use this alongside your php.

 

This way you can do all of the validation relating to dynamic forms on the client side and the php just has to deal with what is actually submitted

Link to comment
Share on other sites

Well you can set it up so that the PHP is at the top of the HTML. That way you can check for wrong values, if something is wrong then the form will be displayed again and you can set the value of the input boxes and the fetched data.

 

So..

 

HTML form --> PHP FORM.

Validation PHP side.

 

IF Validation fails, feed PHP values back to HTML?

 

Here's my code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Survey</title>
</head>

<body>
<p>Impact of Training Form:</p>
<p>Please enter your company name: </p>
<form id="Survey" name="Survey" form action="survey.php" method="post" >
 <label>Company Name
 <input type="text" name="CompanyName" />

 </label>
 <p><strong>Please select your industry type:</strong></p>
 <label>Industry Type
 <select name="IndustryType">
   <option value="1">Select Industry..</option>
   <option value="2">Forestry, Mining, Oil, and Gas extraction</option>
   <option value="3">Finance and Insurance</option>

   <option value="4">Real estate, rental, and leasing ops </option>
   <option value="5">Business services </option>
   <option value="6">Education and health services</option>
   <option value="7">Information and cultural services </option>
   <option value="8">Labour intensive tertiary manufacturing </option>
   <option value="9">Primary product manufacturing </option>

   <option value="10">Secondary product manufacturing </option>
   <option value="11">Capital intensive tertiary manufacturing  </option>
   <option value="14">Construction </option>
   <option value="15">Transportation, warehousing, wholesale </option>
   <option value="16">Communication and utilities  </option>
   <option value="17">Retail trade and consumer services  </option>

   <option value="18">None of the above </option>
 </select>
 </label>
 
 <p><strong>Please select your company size: </strong></p>
 <label>
 <input name="CompanySize" type="radio" value="1" />
Small (less than 50 employees) </label>
 <p>

   <label>
   <input name="CompanySize" type="radio" value="2" />
     Medium (between 50 and 250 employees) </label>
 </p>
 <p>
   <label>
   <input name="CompanySize" type="radio" value="3" />
     Large (greater than 250) </label>

   <br />
 </p>
 <p align="left"><strong>Class Room Training </strong></p>
 <p align="left">If your organization participated in Class Room Training, please indicate so by clicking the appropriate radio button. </p>
 <label>Class Room Training</label>
 <input name="ClassRoomTraining" type="radio" value="1" />
 <label>No Class Room Training</label>

 <input name="ClassRoomTraining" type="radio" value="2" />

 <p><strong>Go to Question 2</strong></p>
 
 <p><strong> Your organization participates in Class Room Training. Please select those that apply: </strong></p>
<label>
<input type="checkbox" name="ClassRoomOrientation" value="1" />
Orientation for new Employees </label>
<p>
 <label>

 <input type="checkbox" name="ClassRoomManagerialOrSupervisoryTraining" value="1" />
   Managerial or Supervisory Training </label>
</p>
<p>
 <label>
 <input type="checkbox"name="ClassRoomApprenticeshipTraining" value="1"/>
   Apprenticeship Training</label>
</p>
<p>
 <label>

 <input type="checkbox" name="ClassRoomComputerHardware" value="1" />
   Computer Hardware </label>
</p>
<p>
 <label>
 <input type="checkbox" name="ClassRoomComputerSoftware" value="1"/>
   Computer Software</label>
</p>
<p>
 <label>

 <input type="checkbox" name="ClassRoomOtherOffice" value="1" />
   Other office and non-office equipment </label>
</p>
<p>
 <label>
 <input type="checkbox" name="ClassRoomGroupDecisionMaking" value="1" />
   Group decision-making or problem solving </label>
</p>
<p>
 <label>

 <input type="checkbox" name="ClassRoomTeamBuilding" value="1" />
    Team-building, leadership, communication</label>
</p>
<p>
 <label>
 <input type="checkbox" name="ClassRoomLiteracyOrNumeracy" value="1" />
   Literacy or Numeracy </label>
</p>

<p><strong>Please estimate the workplaces total training expenditure (in Dollars)</strong></p>

<p>
<label>$

<input name="TotalTrainingExpenditure" type="text" id="TotalTrainingExpenditure" />
</label>
</p>

<p><strong>Which of the following are included in the estimate?</strong></p>

<p>

 <label>
 <input type="checkbox" name="TrainersSalary" value="1" />

   Trainers' Salaries </label>
</p>
<p>
 <label>
 <input type="checkbox" name="TraineesSalary" value="1" />
   Trainees' Salaries </label>
</p>
<p>
 <label>
 <input type="checkbox" name="ContractsToVendors" value="1" />

   Contracts to vendors </label>
</p>
<p>
 <label>
 <input type="checkbox" name="DirectTuition" value="1" />
   Direct Tuition to schools or training institutions </label>
</p>
<p>
 <label>
 <input type="checkbox" name="TrainingMaterials" value="1" />

   Training materials </label>
</p>
<p>
 <label>
 <input type="checkbox" name="TravelOrLivingCosts" value="1" />
   Travel or living costs for trainees and trainers </label>
</p>
<p>
 <label>
 <input type="checkbox" name="Overhead" value="1" />

   Overhead or Office costs for training </label>
</p>

<p>
 <label>
 <input type="checkbox" name="OtherTraining" value="1" />
   Other training expenses </label>
</p>



 <p align="left"><strong>Work Place Training</strong></p>
 <p align="left">If your organization participated in Work Place Training, please indicate so by clicking the appropriate radio button. </p>
 <label>Work Place Training </label>
 <input name="WorkPlaceTraining" type="radio" value="1" />
 <label>No Work Place Training</label>
 <input name="WorkPlaceTraining" type="radio" value="2" />

<br>
<p><strong>Your organization participates in Work Place Training. Please select those that apply:</strong></p>

<br>
<input type="checkbox" name="WorkPlaceOrientation" value="1" />
Orientation for new Employees </label>
<p>
 <label>
 <input type="checkbox" name="WorkPlaceManagerialOrSupervisoryTraining" value="1" />
   Managerial or Supervisory Training </label>

</p>

<p> 
   <label> 
   <input name="WorkPlaceApprenticeshipTraining" type="checkbox" value="1"/>
  Apprenticeship Training  </label>
</p>
<p>
 <label>
 <input type="checkbox" name="WorkPlaceComputerHardware" value="1" />
   Computer Hardware </label>

</p>
<p>
 <label>
 <input type="checkbox" name="WorkPlaceComputerSoftware" value="1"/>
   Computer Software </label>
</p>
<p>
 <label>
 <input type="checkbox" name="WorkPlaceOtherOffice" value="1" />
   Other Office and non-office equipment </label>

</p>
<p>
 <label>
 <input type="checkbox" name="WorkPlaceGroupDecisionMaking" value="1" />
   Group Decision Making or Problem Solving </label>
</p>
<p>
 <label>
 <input type="checkbox" name="WorkPlaceTeamBuilding" value="1" />
   Team-building, leadership, communication </label>

</p>
<p>
 <label>
 <input type="checkbox" name="WorkPlaceLiteracyOrNumeracy" value="1" />
   Literacy or Numeracy </label>
</p>
<p>

<p> </p>
<p><strong>Please estimate the amount of employees that have had workplace training between April of last year and March of this year:  </strong></p>

<p>

 <label>
 <input name="TotalAmountEmployees" type="text" id="TotalAmountEmployees" />
 </label>
</p>


<input type="submit" name="process_form" value="Submit the Form" />

<p> </p>
<p> </p>
<p></p>
</form>

<p> </p>

</body>
</html>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Survey</title>
</head>

<body>
<?php
echo "Impact of Training Form: ";// READ IN DATA FROM HTML FORM

?>

<?php
if( isset( $_POST['process_form'] ) )
{
  $CompanyName = $_POST['CompanyName'];
  $IndustryType = $_POST['IndustryType'];
  $CompanySize = $_POST['CompanySize'];
  $ClassRoomTraining = $_POST['ClassRoomTraining'];
  $ClassRoomOrientation = $_POST['ClassRoomOrientation'];
  $ClassRoomManagerialOrSupervisoryTraining = $_POST['ClassRoomManagerialOrSupervisoryTraining'];
  $ClassRoomApprenticeshipTraining = $_POST['ClassRoomApprenticeshipTraining'];
  $ClassroomComputerHardware = $_POST['ClassRoomComputerHardware'];
  $ClassroomComputerSoftware = $_POST['ClassRoomComputerSoftware'];
  $ClassroomOtherOffice = $_POST['ClassRoomOtherOffice'];
  $ClassroomGroupDecisionMaking = $_POST['ClassRoomGroupDecisionMaking'];
  $ClassroomTeamBuilding = $_POST['ClassRoomTeamBuilding'];
  $ClassroomLiteracyOrNumeracy = $_POST['ClassRoomLiteracyOrNumeracy'];
  $TotalTrainingExpenditure = $_POST['TotalTrainingExpenditure'];
  $TrainersSalary =$_POST['TrainersSalary'];
  $TraineesSalary =$_POST['TraineesSalary'];
  $ContractsToVendors = $_POST['ContractsToVendors'];
  $DirectTuition = $_POST['DirectTuition'];
  $TrainingMaterials = $_POST['TrainingMaterials'];
  $TravelOrLivingCosts = $_POST['TravelorLivingCosts'];
  $Overhead = $_POST['Overhead'];
  $Other = $_POST['Other'];
  $WorkPlaceOrientation = $_POST['WorkPlaceOrientation'];
  $WorkPlaceManagerialOrSupervisoryTraining = $_POST['WorkPlaceManagerialOrSupervisoryTraining'];
  $WorkPlaceApprenticeshipTraining = $_POST['WorkPlaceApprenticeshipTraining'];
  $WorkPlaceComputerHardware = $_POST['WorkPlaceComputerHardwar'];
  $WorkPlaceComputerSoftware = $_POST['WorkPlaceComputerSoftware'];
  $WorkPlaceOtherOffice = $_POST ['WorkPlaceOtherOffice'];
  $WorkPlaceGroupDecisionMaking = $_POST ['WorkPlaceGroupDecisionMaking'];
  $WorkPlaceTeamBuilding = $_POST ['WorkPlaceTeamBuilding'];
  $WorkPlaceLiteracyOrNumeracy =$_POST['WorkPlaceLiteracyOrNumeracy'];
  $TotalAmountEmployees = $_POST ['TotalAmountEmployees'];
  
 
  /// Finished Here
    
}
?>

<br> Name: <?php echo $_POST["CompanyName"]; ?>.<br />
Industry Type: <?php echo $_POST["IndustryType"]; ?>.<br />
Company Size: <?php echo $_POST["CompanySize"]; ?>.<br />
ClassRoom Training: <?php echo $_POST["ClassRoomTraining"]; ?>.<br />
<br>
ClassRoom Options
Orientation: <?php echo $_POST["ClassRoomOrientation"]; ?>.<br />
Managerial or Supervisory Training: <?php echo $_POST["ClassRoomManagerialOrSupervisoryTraining"]; ?>.<br />
Apprenticeship Training: <?php echo $_POST["ClassRoomApprenticeshipTraining"]; ?>.<br />
Computer Hardware Training: <?php echo $_POST["ClassRoomComputerHardware"]; ?>.<br />
Computer Software Training: <?php echo $_POST["ClassRoomComputerSoftware"]; ?>.<br />
Other office and non office equipment: <?php echo $_POST["ClassRoomOtherOffice"]; ?>.<br />
Classroom Decision and Problem Solving: <?php echo $_POST["ClassRoomGroupDecisionMaking"]; ?>.<br />
Classroom Team Building: <?php echo $_POST["ClassRoomTeamBuilding"]; ?>.<br />
Classroom Literacy or Numeracy: <?php echo $_POST["ClassRoomLiteracyOrNumeracy"]; ?>.<br />
<br>
Total Training Expenditure: <?php echo $_POST["TotalTrainingExpenditure"]; ?>.<br />
Trainers Salary: <?php echo $_POST["TrainersSalary"]; ?>.<br />
Trainees Salary: <?php echo $_POST["TraineesSalary"]; ?>.<br />
Vendor Contracts: <?php echo $_POST["ContractsToVendors"]; ?>.<br />
Direct Tuition: <?php echo $_POST["DirectTuition"]; ?>.<br />
Training Materials: <?php echo $_POST["TrainingMaterials"]; ?>.<br />
Travel or Living expenses: <?php echo $_POST["TravelOrLivingCosts"]; ?>.<br />
Overhead: <?php echo $_POST["Overhead"]; ?>.<br />
Other: <?php echo $_POST["Other"]; ?>.<br />
<br>
Work Place Orientation: <?php echo $_POST["WorkPlaceOrientation"]; ?>.<br />
Work Place Managerial or Supervisory: <?php echo $_POST["WorkPlaceManagerialOrSupervisoryTraining"]; ?>.<br />
Work Place Apprenticeship Training: <?php echo $_POST["WorkPlaceApprenticeshipTraining"]; ?>.<br />
Work Place Computer Hardware: <?php echo $_POST["WorkPlaceComputerHardware"]; ?>.<br />
Work Place Computer Software: <?php echo $_POST["WorkPlaceComputerSoftware"]; ?>.<br />
Work Place Other Office: <?php echo $_POST["WorkPlaceOtherOffice"]; ?>.<br />
Work Place Group Decision Making: <?php echo $_POST["WorkPlaceGroupDecisionMaking"]; ?>.<br />
Work Place Team Building: <?php echo $_POST["WorkPlaceTeamBuilding"]; ?>.<br />
Work Place Literacy and Numeracy: <?php echo $_POST["WorkPlaceLiteracyOrNumeracy"]; ?>.<br />
Total Amount of Employees: <?php echo $_POST["TotalAmountEmployees"]; ?>.<br />

<p>
<?php
echo $CompanyName . "<br />";
echo $IndustryType . "<br />";
echo $CompanySize . "<br />";
?>
</p>

</body>


</html>

 

Link to comment
Share on other sites

I would say from the php validation point of view I agree with the post above about having it in the same file as the HTML but above it. This is a common method.

 

However, with regards to your second question for question impacts. I would take a look at javascript validation and use this alongside your php.

 

This way you can do all of the validation relating to dynamic forms on the client side and the php just has to deal with what is actually submitted

 

Could you give me a hand. I'm not quite sure what you mean about having the code 'above' it? You guys mean in the header?

Link to comment
Share on other sites

You can structure a file so that it works like this:

 

 

check to see if form has been submitted

 

  if yes > PHP form validation

 

        if passes > success

        if fails > show form again

 

  if no > show form + html

 

 

This can all be in the same php file. If you do a google search for php form validation all of the tutorials will show you how to set up a file in this way.

 

Then you can worry about the javascript element later for altering the form depending on peoples answers.

Link to comment
Share on other sites

Well because you are showing data after the post, you need an extra if statement. Small example:

 

<?php
if(!isset($_POST['submit_button'])){
//Submit not pressed, show form!
$showForm = true;
}else{
//Submit has been pressed.
$showForm = false; //We'll default it at false if they submitted form.
$error = false; //No errors yet...

$name = $_POST['name'];
$email = $_POST['email'];

if($name == ""){
$showForm = true; //Encountered an error, so we want to show the form.
$error = true;
}

if($email == ""){
$showForm = true; //Set to true
$error = true;
}

if($error){
//Did we encounter an error!?
echo "Sorry but we found an error!";
}
}

if($showForm){
?>
Here you can have your HTML form. For your values on the inputs, just echo out $name or $email.
<?php
}
?>

 

You should define the variables if the form hasn't been submitted as well. So set $name to blank, $email to blank etc etc.

 

Hope that made some sense and you can apply it to your code.

 

Good luck.

 

(Sorry about no indentation, I just wrote it in quick reply. :))

Link to comment
Share on other sites

In summary, what they are saying is that you should create a single .php file that contains your form and all the php that does the php form processing.  You make the form post-back on itself, i.e. you have something like: 

 

<form name="form1" method="post" action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>">

 

I think this is probably the best method for you to choose, because it makes it a bit easier to remember the user submitted answers if you have to send the user back to the form.  The variables are all contained in one page. 

 

Rgds

Link to comment
Share on other sites

In summary, what they are saying is that you should create a single .php file that contains your form and all the php that does the php form processing.  You make the form post-back on itself, i.e. you have something like: 

 

<form name="form1" method="post" action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>">

 

I think this is probably the best method for you to choose, because it makes it a bit easier to remember the user submitted answers if you have to send the user back to the form.  The variables are all contained in one page. 

 

Rgds

 

So.

 

Instead of having the survey.html, I have all of that code in my PHP file? And that will work?

I'm still a little confused though.

Link to comment
Share on other sites

Well because you are showing data after the post, you need an extra if statement. Small example:

 

<?php
if(!isset($_POST['submit_button'])){
//Submit not pressed, show form!
$showForm = true;
}else{
//Submit has been pressed.
$showForm = false; //We'll default it at false if they submitted form.
$error = false; //No errors yet...

$name = $_POST['name'];
$email = $_POST['email'];

if($name == ""){
$showForm = true; //Encountered an error, so we want to show the form.
$error = true;
}

if($email == ""){
$showForm = true; //Set to true
$error = true;
}

if($error){
//Did we encounter an error!?
echo "Sorry but we found an error!";
}
}

if($showForm){
?>
Here you can have your HTML form. For your values on the inputs, just echo out $name or $email.
<?php
}
?>

 

You should define the variables if the form hasn't been submitted as well. So set $name to blank, $email to blank etc etc.

 

Hope that made some sense and you can apply it to your code.

 

Good luck.

 

(Sorry about no indentation, I just wrote it in quick reply. :))

 

Wait I think I understand now..

 

So first the user goes to the HTML form.

Submits it..

If $error is triggered, you show the exact same form on the php file. Until the form is error free, it does not submit the values.

 

Right?

Link to comment
Share on other sites

Instead of having the survey.html, I have all of that code in my PHP file? And that will work?

I'm still a little confused though.

 

Yes, you have a single php file that does everything.  I suggest you call it survey.php. 

 

In the top section of this file will be a <?php ?> section that controls processing of the form when it has been submitted (as can be ascertained by some kind of flag as discussed previously). 

 

In the bottom section of this file is you <form> </form>.

 

The php code will have some kind of error checking routine whereby if there are errors they are shown to the user and the script "falls back" to show the form again, although this time the fields can be populated with what the user has already put in. 

 

If the form contains no errors, then the script can move on to a database routine which puts the data into your database. 

 

Rgds

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.