1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

HTML/PHP Form help (n00b)

Discussion in 'HTML, Graphics & Programming' started by Sone, 15 Jun 2006.

  1. Sone

    Sgarrista

    Joined: 18 Oct 2002

    Posts: 7,669

    Writing my first page with a form that submits to a php script (sorry if terminolgy is wrong), the php error checks and puts an error message at the top if there is a problem with the submitted data but it always clears the form, how can i get the form to stay the same if theres an error?
     
  2. elkdanger

    Wise Guy

    Joined: 11 Mar 2003

    Posts: 1,328

    Location: North Shields

    I would create the form and set the value of the form fields to themselves. If that doesn't make much sense, this is what I mean:

    Code:
    <form method='post' action='somescript.php'>
    <input name='txt_example' value=" <?php @echo($_POST['txt_example']); ?>">
    </form>
    
    
    Points to note here is that:
    * You pick up the same name in the Post array that you give the input element
    * You use the @ suppress error command, because when you first load the page 'txt_example' won't exist in the $_POST array since you won't have submitted the form yet.

    That should work.
     
  3. Sone

    Sgarrista

    Joined: 18 Oct 2002

    Posts: 7,669

    thanks mate, will have a look now. But on a successfull sumbit won't that still keep fields the same?
     
    Last edited: 15 Jun 2006
  4. elkdanger

    Wise Guy

    Joined: 11 Mar 2003

    Posts: 1,328

    Location: North Shields

    Mmm yes it will. What are you planning to do after the form has been submitted though? Leave the form there for another entry to be submitted?

    What I would normally do with a form is:

    > present the form to the user, setting the form action to be the same page, but with a parameter entered into the querystring to notify the page that it should process form input.

    > After submission, the form gets processed and the data is used for whatever it's being used for

    > Redirect the user somewhere else if everything went ok. If not, continue on and present the form again.

    If you just want to present the form again even on a successful submission then there's probably someway of unsetting the $_POST array which will make all the fields blank in my example code above. If not, you could store the $_POST elements into an intermediate array and unset those before the form is presented.
     
  5. Dj_Jestar

    Caporegime

    Joined: 18 Oct 2002

    Posts: 29,080

    Location: Back in East London

    I'd probably go a bit more elaborate than that, to avoid using error suppression and also to cleanse data. Probably use some Javascript too.

    edit: untested..
    Code:
    <html>
        <head>
            <title>Test</title>
        </head>
        <body>
            <script language="JavaScript">
            
            var values = new Array();
    <?php
    foreach ($_POST as $key => $val) {
        echo "\t\t\t\t\t\tvalues[\"$key\"] = '" . htmlentities($val) . "',\n";
    }
    ?>
            function fillValue (obj) 
            {
                if (arrayKeyExists(obj.name, values)) {
                    obj.value = values[obj.name];
                }
                return false;
            }
            </script>
            <form action="" method="post">
                <input type="text" name="text1" onLoad="fillValue(this)" />
                <input type="text" name="text2" onLoad="fillValue(this)" />
                <input type="submit" />
            </form>
        </body>
    </html>
    add a whitelist if() to the foreach loop to ensure the values echoed are only those in the form.
     
    Last edited: 16 Jun 2006
  6. elkdanger

    Wise Guy

    Joined: 11 Mar 2003

    Posts: 1,328

    Location: North Shields

    That's a nice way of doing it :) Perhaps a bit over elaborate for me, but it's a means to the same end, really.
     
  7. Dj_Jestar

    Caporegime

    Joined: 18 Oct 2002

    Posts: 29,080

    Location: Back in East London

    it won't work exactly like that anyway.. onLoad is not a valid event for <input> objects :(

    If you wanna get real fancy, you could AJAX it..