How to create a request for a server-side script form that has characteristics on the client side?

I work in PHP to create a form. I know how to display the form and then accept the submitted values ​​from the $ _POST variable, and I know how to check these variables and display the "Thank you" or "Error" page depending on the input.

However, I don’t know how to do this, to create a client system in which, despite the fact that my users press the back button on a separate screen, I can take the information that I collected from the first view and display dynamic error messages of type "Please enter a valid email address" or "Name - required field" next to the fields that were entered incorrectly. I would also like to get all the previously submitted data that was valid, and fill it out in a form so that users do not get upset, losing everything that they entered.

What is the correct approach to doing something like this in PHP? Initially, I thought that if I could pass an array of error messages with an input type tag = "hidden", I could then pull my values ​​out and dynamically display the messages using PHP, but I get stuck with this approach all the time.

+1
source share
3 answers

You can add errors to the php session, but this creates problems for users who have several browser tabs.

My preferred method is to submit the form to the same page and place the errors directly on this page so that the user does not have to click the back button. This way you can highlight the fields directly in the form (make the background or path red or something like that.)

<input type="text" <?php (empty($_POST['field']?'style="backgroung-color: red;"':''))?> name="field" value="<?php echo $_POST['field']?>" /> 

You can put <input type="text" name="field" value="<?php echo $_POST['field']?>" /> To get the old value.
Since the web is, by definition, stateless, there is no really good way to track what the user does when they click the back button. There are hacks that work using a hidden iframe, but this is more of a problem than what you are looking for.

+3
source

Do not mix client logic with server logic. Exactly the same script can output the form and accept it. If the entry is successfully verified, it continues. If not, it will display the form again, this time with error messages and already entered data.

The next time the user submits the form, validation starts again until it passes successfully.

Thus, you expand the form with input values ​​and error messages in the first place, but you only show them if they are marked / installed.

This can only be done with additional variables next to $_POST - or if you like it - using a complete abstraction of the form from the framework, such as the zend framework (which can be overhead for what you like) or just with the library / component, similar to the popular HTML_QuickForm2 .

Edit:

This is very simple code to demonstrate the general methodology, if you use the library, it is much nicer (and you do not need to code it, instead you can focus on the actual form, like the definition above). This code is more for reading and understanding the stream than for using, I quickly typed it so that it (certainly has) syntax errors, and it is not complete for a full-blown form. This has only one email field and there is even no submit button:

 /* setup the request */ $request->isSubmit = isset($_POST['submit']); /* define the form */ $form->fields = array ( 'email' => array ( 'validate' => function($value) {return filter_var($value, FILTER_VALIDATE_EMAIL);}, 'output' => function($value, $name) {return sprintf('<input type="text" value="%s" id="%s">', htmlspecialchars($value), htmlspecialchars($name)}, 'default' => ' info@example.com ', ), ); /** * Import form data from post request * * @return array data with keys as field names and values as the input strings * or default form values. */ function get_form_post_data($form, $request) { $data = array(); foreach($form->fields as $name => $field) { $data[$name] = $field->default; if ($request->isSubmit && isset($_POST[$name])) { $data[$name] = $_POST[$name]; } } return $data; } /** * Validate form data */ function validate_form_data($form, $data) { foreach($form->fields as $name => $field) { $value = $data[$name]; $valid = $field['validate']($value); if (!$valid) { $form->errors[$name] = true; } } } function display_form($form, $data) { foreach($form->fields as $name => $field) { $value = isset($data[$name]) ? $data[$name] : ''; $hasError = isset($form->errors[$name]); $input = $field['output']($name, $value); $mask = '%s'; if ($hasError) { $mask = '<div class="error"><div class="message">Please Check:</div>%s</div>'; } printf($mask, $input); } } // give it a run: # populate form with default values -or- with submitted values: $form->data = get_form_post_data($form, $request); # validate form if there is actually a submit: if ($request->isSubmit) { validate_form_data($form, $form->data); } # finally display the form (that can be within your HTML/template part), works like echo: display_form($form, $form->data) 
+3
source

Use the form to submit to the same page, and if the form is being verified, use the header to redirect the user to the thank you page.

 header("Location: thank-you.php"); 

If the form fails validation, you can easily display all errors on one page.

0
source

All Articles