Jump to content

Form Validation on Postback


webref.eu

Recommended Posts

Hi All

 

I am posting back a form to itself and trying to validate it with php, as per the code snippet given below.  I have used a flag called processform to make sure the form isn't processed when first loaded, only when first submitted. 

 

How do I handle the validation and reshow the form again if necessary?  Below you'll see I have code that checks all fields are filled in.  If they aren't it currently gives a die statement.  However, what I want it to do is show an error message and then reshow my original form with the input that the user gave.  I guess I should be using something other than the die statement as this just stops everything. 

 

Any guidance or links to relevant tutorials very welcome. 

 

Thanks all.

 

 

//CODE STARTS

 

//If processform flag is set, processing can occur.  If not set, form is being loaded for the first time processing

if ($_POST['processform'] == 1) {

 

 

//get User details

$Username=$_POST['txtUsername'];

$Password=$_POST['txtPassword'];

$ConfirmPassword=$_POST['txtConfirmPassword'];

$Email=$_POST['txtEmail'];

$ConfirmEmail=$_POST['txtConfirmEmail'];

$SubscribeToNewsletter = $_POST['chkSubscribeToNewsletter'];

 

//do processing to validate fields

 

/* Make sure all fields were entered */

  if (!$Username | !$Password | !$ConfirmPassword | !$Email | !$ConfirmEmail) {

      die('You didn\'t fill in a required field.');

}

 

//CODE ENDS

Link to comment
https://forums.phpfreaks.com/topic/116729-form-validation-on-postback/
Share on other sites

How about using a function to display the form, like the code below. You'll have to change the function to represent the actual form your using.

 

<?php
function display_form($error = null)
{
// You'll need to change the line below to represent your actual form
$form =  '<form action="" method="post">';
// If an error message is passed through the function...
if($error != null)
{
	// Show it.
	$form .= '<div class="error">'.$error.'</div>';
}
// You'll need to change this bit to represent your actual form
$form .= '<input type="text" name="username" />
<input type="text" name="password" />
etc, etc, etc
<input type="submit" name="processform" />
</form>';
return $form;
}

//If processform flag is set, processing can occur.  If not set, form is being loaded for the first time processing
if (isset($_POST['processform'])) {
//get User details
$Username=$_POST['txtUsername'];
$Password=$_POST['txtPassword'];
$ConfirmPassword=$_POST['txtConfirmPassword'];
$Email=$_POST['txtEmail'];
$ConfirmEmail=$_POST['txtConfirmEmail'];
$SubscribeToNewsletter = $_POST['chkSubscribeToNewsletter'];

//do processing to validate fields

/* Make sure all fields were entered */
   	if (!$Username | !$Password | !$ConfirmPassword | !$Email | !$ConfirmEmail) {
    	echo display_form('You didn\'t fill in a required field!');
}
else
{
	// Add the user
}
}
else
{
// No errors to display, so we just do it like this:
echo display_form();
}
?>

Thanks for your comments.  I have just thought of using an $ErrorMsg variable to control the form processing, which seems to work just as I want it to, so I think I'm going to try this first, the code being: 

 

//CODE STARTS

 

//do processing to validate fields

 

// create empty error variable

$ErrorMsg = "";

 

/* Make sure all fields were entered */

  if (!$Username | !$Password | !$ConfirmPassword | !$Email | !$ConfirmEmail) {

      //die('You didn\'t fill in a required field.');

  $ErrorMsg = "Missing field";

}

 

 

echo $ErrorMsg;

 

 

If ($ErrorMsg == "") {

 

//add new user to database

 

}

 

//CODE ENDS

 

Rgds

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.