Jump to content

question regarding form


jonleow

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.

Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.