Thursday, March 24, 2011

How do you post the contents of form to the page in which it is?

In Yahoo or Google and in many websites when you fill up a form and if your form has any errors it gets redirected to the same page. Note that the data in the form remains as it is. I mean the data in the text fields remains the same. I tried ‹form action="(same page here)" method="post or get"›. It gets redirected to the page, but the contents of the form gets cleared. I want the data to be displayed.

You know how tiresome it will be for the user if he has to fill up the entire form once again if he just forgets to check the accept terms and conditions checkbox. Need help!

From stackoverflow
  • You'll have to check the data within the same file, and if it is correct, then you redirect to the correct location. Then you can use the $_POST or $_GET information the user posted and he can fix the error(s).

  • You need to do this yourself. When the page gets posted you'll have access to all the form values the user entered via $POST['...']. You can then re-populate the form fields with this data.

  • This is not done automatically. They get values from post / get and then assign the values the user typed to the template. What you see is html that was generated from the script that handled user values.

  • You can use two approachs (they're not mutually exclusive):

    • Use JavaScript to help the user before he submits the form. That way, you save a roundtrip to the server. What you asked for:
    • In the form, fill the value attributes of the fields with the data sent back from the server. For example: you send a field name, which you get as $_POST['name'] in PHP (assuming you used method='post'. If you send back the data and modify that field adding value='<?php $_POST['name']; ?> you should get your data back.
  • If you put your form and the form data processing in the same script, you can easily print out the data that has already been entered in the form, e.g.:

    $valid = false;
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (isset($_POST['name']) && $_POST['name'] == 'Hugo') {
            $valid = true;
        } else {
            echo '<p>Seriously, you have to enter "Hugo"!</p>';
        }
        // more data processing
        if ($valid) {
            echo '<p>Everything’s fine!</p>';
        }
    }
    if (!$valid) {
        echo '<form action="" method="post">';
        echo '<p>Please enter "Hugo": <input type="text" name="name" value="', (isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''), '"></p>';
        echo '<p><input type="submit"></p>';
        echo '</form>';
    }
    

    Well this is not nice example but that’s how it works.

  • a lot of frameworks do this job for you, so dont waste your time doing this manually

  • Here is a modified version of what I use for very simple websites where I don't want/need an entire framework to get the job done.

    function input($name, $options = array()) {
     if(!isset($options['type'])) $options['type'] = 'text';
    
     $options['name'] = $name;
    
     if(isset($_POST[$name]) && $options['type'] != 'password') {
      $options['value'] = htmlspecialchars($_POST[$name]);
     }
    
     $opts = array(); 
     foreach($options as $key => $value) {
      $opts[] = $key . '="' . $value . '"';
     }
    
     return '<input ' . implode(' ', $opts) . '/>';
    }
    

    (I have a few similar functions for <select> and <textarea> and so on)

    When you're building fields you can do something like:

    First Name: <?=input('first_name')?>
    Last Name: <?=input('last_name')?>
    Password: <?=input('password', array('type' => 'password'))?>
    

    If you process your forms in the same page as the form itself, they will get auto filled if there are any errors. Most frameworks, though, do all of this for you (and in a much better way than the code above), I personally suggest CakePHP or CodeIgniter.

  • If you're using a template or framework system (I've incorporated the Smarty engine into several projects of mine), you can usually tweak the templates so they automatically fill fields with values if they detect that the $_POST[$variable] value corresponding to their field is set.

    As for the passwords, as far as I understand it (I could be wrong): it's a convention that minimizes the amount of time that password is being sent over the wire, hence shrinking the window for anyone who may be sniffing to pick up on the text. It's just good practice to leave password fields blank, is all.

0 comments:

Post a Comment