Jump to content

Recommended Posts

i'm trying to validate a form and am looking to send the user back so they can retry their form submission if there are any errors,

 

I'm collecting errors like this:

 

if (empty($mobileTel) || !(int)$mobileTel || strlen($mobileTel < 5))
{
$errors[] ='Please enter a valid mobile number';
}

 

and displaying the errors like this:

 

   if(isset($errors))  
     {  
         if(is_array($errors))
        	{
        		
              echo '<p class="error"><b>The following errors occured:</b></p>';
        		  while (list($key,$value) = each($errors))
          		{
         			echo '<span class="error">-'.$value.'</span><br />';
        		  }echo"<br />";
        		
        	 }  
     }  
else {

 

ideally I want a script that will check to see isset($errors) and if they are then to display them.

 

Is there a way I can send $errors back to where they came from i.e. header ('Location: form.php), but if I do this then surely there is no way of knowing the errors that have been sent.

 

hope this is clear enough and any feedback welcome

Link to comment
https://forums.phpfreaks.com/topic/66180-sending-data-not-with-post-or-get/
Share on other sites

I have always thought the best way to do validation is to have a single page that has the form, validation and procesing included (albeit in included files). Then you just have some conditional statemetns to decide which code you want to run.

 

Main page

<?php

//determine if page was posted
if (isset($_POST{'submit')) {

  $error = false;
  //Include the validation script here
  //Set $error to an error message text if any validation fails

}

if ($error === false) { //Form was posted and passed validation

  //Include processing script here

  //redirect to confirmation or next apge
  header ("Location: confirmationpage.php");

}

//Include form here using $_POST values in the fields

?>

<input type="text" name="username" value="<?php echo $_POST['username']; ?>">

There isn't really a way to do this that isn't complicated.

I agree with mjdamato (my session timed out while posting and you beat me, but I'm posting it anyway!):

 

What I would recommend is use the same php file to print out the form as you do to check it for errors. Before printing out the form, check if the php file received form information, if it has, check it for errors, if there errors, print the errors and the form, if no errors, success!

 

To do it your way you might need to serialize the array as into a session or post a huge string. You CAN get the $_GET array to accept an array from the URL, but I don't think you can put more than one value into the array, you might find something about that on google.

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.