Jump to content

Archived

This topic is now archived and is closed to further replies.

jonleow

question regarding form

Recommended Posts

Hi, i have a php file which validate missing input in a form. The code is as follows:

--------------------------------------------------------------------------------------------
<?php
$submit = (mysql_escape_string($_POST['submit']) == "Submit Now") ? true : false;
$firstName = mysql_escape_string($_POST['firstName']);
$middleName = mysql_escape_string($_POST['middleName']);
$lastName = mysql_escape_string($_POST['lastName']);

if ($submit) {
if ($firstName == "") {
echo "Please enter your firstName";
}
elseif ($middleName =="") {
echo "PLease enter your middleName";
}
elseif ($lastName == "") {
echo "Please neter your lastName";
}
exit();
}
?>


else {
echo "Welcome $firstName $lastName";
}
--------------------------------------------------------------------------------------------

What I wanna do is that i want to provide a link back to my form and i do not want those fields the users have typed in to be empty. What the users keyed in remain and i want the users continue keying the fields they accidentally left out.

Share this post


Link to post
Share on other sites
I use the following, and this doesn't clear the form, and allows the user to go back to the form page with all their answers intact:

[code]
echo '<FORM><INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>';
[/code]

Share this post


Link to post
Share on other sites
The easiest way (IMHO) to do this is to combine the script that processes the form and the script that displays the form into one script. Not knowing what your form really looks like I'm going to guess.

Try something like this:
[code]<?php
$errs = array();
if (isset($_POST['submit'])) { // form was submitted, check values
        foreach($_POST as $key => $val)
                switch($key) {
                   case 'firstName':
                   case 'middleName':
                   case 'lastName':
                        if (trim(stripslashes($val)) == '')
                           $errs[] = 'Please enter your ' . str_replace('Name',' name',$key);
                        else
                           $$key = $val;
                        break;
                }
        if (!empty($errs)) unset($_POST['submit']);
        else echo 'Welcome ' . $firstName . ' ' . $lastName;
}
if (!isset($_POST['submit'])) { // can't use "else" here, since we may have done the "unset" above
        $tmp = array();
        if (!empty($errs)) {
            $tmp[] = 'The following errors occured during the processing of your input data<br>';
            foreach ($errs as $emsg)
                $tmp[] = '<span style="color:red">' . $emsg . '</span><br>';
            $tmp[] = '<hr>'; }
        $tmp[] = '<form method="post">';
        $tmp[] = 'First Name:<input type="text" name="firstName"' . disp_value('firstName') . '<br>';
        $tmp[] = 'Middle Name:<input type="text" name="middleName"' . disp_value('middleName') . '<br>';
        $tmp[] = 'Last Name:<input type="text" name="lastName"' . disp_value('lastName') . '<br>';
        $tmp[] = '<input type="submit" name="submit" value="Login Now">';
        $tmp[] = '</form>';
        echo implode("\n",$tmp)."\n";
}

function disp_value($str)
{
        if (!isset($_POST[$str])) return;
        return(' value="' . trim(stripslashes($_POST[$str])) . '"');
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.