jonleow Posted April 13, 2006 Share Posted April 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/7322-question-regarding-form/ Share on other sites More sharing options...
hadoob024 Posted April 13, 2006 Share Posted April 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/7322-question-regarding-form/#findComment-26737 Share on other sites More sharing options...
kenrbnsn Posted April 13, 2006 Share Posted April 13, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7322-question-regarding-form/#findComment-26755 Share on other sites More sharing options...
jonleow Posted April 14, 2006 Author Share Posted April 14, 2006 thanks guys...i'll try out your scripts Quote Link to comment https://forums.phpfreaks.com/topic/7322-question-regarding-form/#findComment-26848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.