SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to save form data and then re-populate on "submit"

    I have seen this done on various websites so I know it can be done. I have a php file that contains a form with several data entry fields and a section where the results of calculations based on that input data are displayed. What I would like to have happen is when the user clicks the "submit" button on the form php file is redisplayed but with the entry data that was supplied still there (not the default value).

    I have actually been able to accomplish this by making a !isset($_COOKIES[]) and setcookies calls for each of the form fields, then have the form 'submit' execute a different php file that sets the COOKIES from the form POST data, then redirects back to the origional php file. The problem with this is that I have will have to have an !isset block for each of the form fields then in the alternate file, I will have to have another set of setcookies calles. This very tedious, and I'm sure there is a better way.

    The websites I have found that do this, when I look at the source code (initially before any form entries are made) lists the value of the form fields as "" (i.e. value=""). Then after entering data and clicking submit, that same source code lists the values as the the data that was entered (i.e. value="100"). The form is set to execute a a different php file called cookies.php (action="cookies.php"). Unfortunately I am not able to see the source code for the cookies.php file, so I don't know what is going on there.

    Any assistance would be appreciated.

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you please give us more detailed explanation of your problem?

  3. #3
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just figured it out. Thanks.

  4. #4
    SitePoint Evangelist Dave Morton's Avatar
    Join Date
    Sep 2003
    Location
    Carson City, NV
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm glad you figured it out, but for the benefit of others here who may have the same problem, a bit more info would be appreciated.
    Making a difference, one little psychotic episode at a time
    Geek Cave Creations
    Beta testers needed for pChat
    Dave's Gallery

  5. #5
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a code snipit with the solution.

    <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
    <input type="text" name="name" value="<?php echo $_POST['name'] ?> " />
    <input type="submit" value="submit" name="submit" />
    </form>

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then create a function to make a texfield:
    PHP Code:
    <?php
    function textfield($label $name$type="text"){
        if(isset(
    $_POST[$name])) 
        
    $_COOKIE["saved"][$name] = $_POST[$name];
    // if its not working then try     setcookie("saved[$name]", $_POST[$name]);
    // or you can save these values in $_SESSION instead.    
        
    $value=isset($_COOKIE["saved"][$name]) ? htmlspecialchars($_COOKIE["saved"][$name]):'';
        echo 
    $label ':<input type="'.$type.'" name="'.$name.'" value="'.$value.'" />';
    }

    // then you need textfields 
    textfield("Name" "name");
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •