• I have a form and it has checks in place that make sure that required fields are filled in. If the checks fail, the user is redirected to the original form. I use the $_POST data and re-fill in the completed parts of the form using URL parameters. The problem is that when the $_POST data is added to the redirect URL as a parameter, the spaces in the field are scrubbed.
    e.g.
    Form field ‘question’ is filled in with “this is a test” and processed as

    $qtitle = $_POST['question'];

    Check fails and the user is redirected like so
    wp_redirect('https://www.example.com/submit-question/?err=2&qtl='.$qtitle);

    The form then redisplays the form field ‘question’, pulling from the qtl parameter, as “thisisatest”

    Why is it stripping the spaces and how can I fix it?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Adding anything to the URL should be done using urlencode() and urldecode() otherwise things may not be transferred correctly.

    In your example, you’d use:

    $qtitle = urlencode($_POST['question']);
    wp_redirect('https://www.example.com/submit-question/?err=2&qtl='.$qtitle);

    And then:

    $qtitle = urldecode ($_GET['qtl']);
    echo $qtitle;
    Thread Starter hannamyluv

    (@hannamyluv)

    Thank you for the information!

    I ended up not using the second urldecode when retrieving the data because it added + signs where the spaces were. But other than that, it worked great. Thanks!

    urldecode() removes those. It’s the reverse of urlencode().

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redisplaying $_POST data’ is closed to new replies.