HTML/PHP Form help (n00b)

Soldato
Joined
18 Oct 2002
Posts
7,844
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?
 
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.
 
thanks mate, will have a look now. But on a successfull sumbit won't that still keep fields the same?
 
Last edited:
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.
 
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:
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..
 
Back
Top Bottom