Jump to content


Photo

question regarding form


  • Please log in to reply
3 replies to this topic

#1 jonleow

jonleow
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 April 2006 - 03:28 PM

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.



#2 hadoob024

hadoob024
  • Members
  • PipPipPip
  • Advanced Member
  • 192 posts

Posted 13 April 2006 - 08:43 PM

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:

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


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 April 2006 - 09:16 PM

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:
<?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])) . '"');
}
?>

Ken

#4 jonleow

jonleow
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 April 2006 - 03:32 AM

thanks guys...i'll try out your scripts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users