Grow your CSS skills. Land your dream job.

How do I keep or retain my form values even if the form has been submitted?

  • # April 11, 2013 at 4:14 am

    I have a form that submits its values to itself. I want the values to stay printed so the user will not try to right all over again what he/she has previously typed.

    # April 11, 2013 at 1:12 pm

    When you receive the form submission, save the valid values and print them when you print the form again.

    < ?php
    # check if form was submitted
    if( isset( $_POST ) ){
    # validate submitted value
    $text = some_func_to_validate( $text )?
    $text:
    null;
    }
    # if $text was submitted & valid,
    # use it for form value
    $text_value = $text?
    $text: // submitted value
    “”; // empty string
    # print form
    print < <

    # April 11, 2013 at 10:04 pm

    Thanks @traq for that. I just wonder, Should it output any error if there is no validation used? I got the idea from your code. I just used the isset to check if the form was submitted then declared my variables and used them as field value.

    Will I encounter any problems with this?

    if( isset( $_POST ) ){

    $title = $_POST;
    $editor_data = $_POST[ 'editor1' ];
    $format = $_POST[ 'format' ];

    }

    my form:


    Select Title Format


    # April 12, 2013 at 1:45 am

    Have you thought about using localStorage to save form values?

    # April 12, 2013 at 2:14 am

    > Should it output any error if there is no validation used? *[...]* Will I encounter any problems with this?

    At a **minimum**, you need to be sure you don’t output any HTML or JavaScript with the values (use [`strip_tags()`](http://php.net/strip_tags) or [`htmlentities()`](http://php.net/htmlentities)) as a safeguard against XSS attacks.

    > Have you thought about using localStorage to save form values?
    @JohnMotylJr

    I agree that it’s good to use localstorage *also*, but doing it in PHP is still practical (since you have to do server-side validation anyway) and convenient for the user (since you return *only* the valid values).

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".