When the form validation fails, how do I pass the error information for a new attempt?

I am developing a form validation class in PHP. When form validation fails, I can redirect to the html page of the form again, but without error information. I would like to redirect to the form page with specific errors about which fields failed and why.

How should I do it? Should I send information via GET or POST? and in what format? It would really be to see some code to see how people solved this problem.

Thanks!

+4
source share
6 answers

You can use the header () function. So just check the fields that are laid out:

if(!$form->valid()){ $msg = "Form is not valid"; } else { //Don't know if you want this $msg = "Form is valid"; } header("Location: [page where you came from]?msg=" . urlencode($msg)); 

Then on the page where you redirect to use

 if(isset($_GET['msg])) echo urldecode($_GET['msg']); 

for echo messages. If you use other get variables in the header function location, of course, use &msg=" . urlencode($msg) . (You can also return the values ​​that the user submitted, so the user does not need to fill out the entire form again if he makes 1 error.

+2
source

I agree with the suggestion of user187291 to use $_SESSION , because:

  • This doesn’t carry URIs like with $_GET (you will never need a static link to a status message). Users can click back to the page with your form and still see the status message because the URI says so.
  • Print and disable it in the same mode, you won’t be able to use it more than once (is that what you want ?!)

If you are going with AJAX, $_GET more commonly used to get the values ​​you make from the validation controller.

+2
source

there are a number of approaches

  • pass errors to GET when redirecting back as you said
  • use sessions to store error information; on the form page check the session for errors
  • do not redirect after failure, just display the form with error messages
  • ajax sends

which can be used depends on the application. For most applications, the session method is most suitable.

+1
source

Something like that:

// Pseudocode

 function isValid($parm) { $return = false; if(preg_match(/^[a-zA-Z]+$/, $parm) { $return = true; } return $return; } $firstname = $_GET["fname"]; $lastname = $_GET["lname"]; $validFirstName = isValid($firstname); $validLastName = isValid($lastname); if($validFirstName == true && $validLastName == true) { echo "It all good"; // Do what you need to like, Submit } else { echo "Please retry"; // Display error message } 
0
source

I use a class to interact with $ _POST, similar to the following:

 // create the object $post = new PostData(); // register your requirements... write whatever methods you need // for each call,read $_POST, check criteria, and mark the field // as good or bad... $post->required ('LastName'); $post->required ('FirstName'); $post->numeric ('Age'); $post->optional ('MiddleInitial'); $post->regExp ('/\d{3}/','AreaCode'); $post->email ('Email'); // check the status if (!$post->isValid ()) { $_SESSION['FailedPostData'] = $post; header ('Location: page.php'); } // normal form processing 

Then on the page. php you can see if there is a FailedPostData in the session, read it to find the information entered last time, as well as which fields failed. I use a macro template engine that allows me to easily re-populate form inputs and flag failures. Otherwise, you can get a lot of code for a simple form ...

You will also need a mechanism to ensure that deprecated FailedPostData do not hang in the session and confuse things.

0
source

I do it like this. New to php, so not sure if this is the best way to do:

HTML form page:

 <form id="abc" method="post" action="validate.php"> 

PHP page .. validation conditions. Any function if things do not match

 function display_error($error) { echo "<html><body><link href='style.css' rel='stylesheet' type='text/css'><br><center><h2>"; echo "$error"; echo "</h2></center><br><br>"; echo "<center><input type='button' value='Go Back' onClick='history.go(-1)' style='width:100px; height:28px; font-size:16px'></center>"; echo "</body></html>"; } 

By clicking on the "Back" button, you will return to the html page with the data intact.

0
source

All Articles