garry Posted June 1, 2008 Share Posted June 1, 2008 So I have a form that submits user supplied data which is then checked for a few things and (if successful) added to the database. The only problem is that if something goes wrong with the users input (they forget to fill in a field) the error message will be displayed, they will click "Back" and all of the information they put in is gone! For example, this is how I check the data: if (empty($_POST['artist']) || empty($_POST['description'])) { echo "Oops! Looks like you haven't filled in all of the fields. Go back and try again."; } Then they click on Back and it's all blank. I know this is going to be very frustrating if it happens to anyone as they are inputting reviews and a lot of formatting so they will not be happy if they have to do it again! How can I fix this problem? Thanks! Quote Link to comment Share on other sites More sharing options...
radar Posted June 1, 2008 Share Posted June 1, 2008 In regular PHP i'm not sure.. you might be able to do something like $_SESSION['post'] = $_POST; then in the form fields do something like $_SESSION['post']['first_name'], etc for every field in your form... alternative: use smarty.. Quote Link to comment Share on other sites More sharing options...
garry Posted June 1, 2008 Author Share Posted June 1, 2008 Surely there's an easier way then that? Because I've seen it done in a lot of places. Hell, even the forums here do it. And I've heard of Smarty as a template system? I don't want to use that, I'm trying do all of it myself. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 1, 2008 Share Posted June 1, 2008 In my opinion the best way to handle this is to use the same page for the form and for validation. That way if a piece of validation fails you can show the form with the input the user already submitted. Here's an incomplete script that shuld show the process: <?php if (isset($_POST['submit']) { $errors = array(); //validate form if ($_POST['name']=='') { $errors[] = 'You must enter a name.'; } if ($_POST['phone']=='') { $errors[] = 'You must enter a phone number.'; } //If no errors, insert data if (count($errors)==0) { //Isert data to db and redirect to next page header("Location: confirmation_page.php"); exit(); } } //Show the form ?> <?php if (count($errors)>0) { echo "The following errors occured:<br>"; echo "<ul>"; echo '<li>' . explode('</li><li>', $errors) . '</li>'; echo "</ul>"; } ?> Name: <input name="name" value="<?php echo $_POST['name'] ; ?>"> Phone: <input name="phone" value="<?php echo $_POST['phone'] ; ?>"> Quote Link to comment Share on other sites More sharing options...
chronister Posted June 1, 2008 Share Posted June 1, 2008 Surely there's an easier way then that? HAHAHAHA I wish. Proper form validation with errors displayed nicely is one of the most tedious tasks I have found in PHP. You should see the complexity of some of the form validation I have had to create in order to make sure the user is not pissed off when they don't fill it out right. I have a strong opinion about this subject. If there is an error in the submitted info, DON'T MAKE YOUR USER HIT THE BACK BUTTON. Display the form again with the information they filled out already pre-filled in the form and clear error messages or field highlighting. The following example is not my best work. It is a project timeclock for my use, so I skimped on the error messages and field highlighting, but hopefully you get the point. form processing script <?php if(isset($_POST['add_project'])) { $project_name=$_POST['project_name']; $clientId=$_POST['clientId']; $project_description=$_POST['project_description']; $start_date=$_POST['start_date']; $end_date=$_POST['end_date']; $rate=$_POST['rate']; if(empty($project_name) || empty($clientId) || empty($project_description) || empty($start_date) || empty($end_date) || empty($rate)) { $error = 'Please fill out all information'; // error detected } else { $names = array('project_name', 'clientId', 'project_description', 'start_date', 'end_date', 'pay_rate'); $values = array($project_name, $clientId, $project_description, $start_date, $end_date, $rate); $results = $db->sqlInsert($names, $values, 'projects'); $message = 'Added '.$results['inserted'].' Project'; } } ?> form filler outer <?php function checkSet($fieldname) { global $error; if(isset($error)) { if(isset($fieldname)) { echo 'value="'.$fieldname.'"'; } } } ?> Sample form field <div> <input id="start_date" name="start_date" class="element text small" type="text" maxlength="255" <?php checkset($start_date); ?>/> </div> <label class="description" for="end_date">End Date </label> <div> <input id="end_date" name="end_date" class="element text small" type="text" maxlength="255" <?php checkset($end_date); ?>/> </div> The form's action is to itself, the processing script is in an include file specifically for the projects form, and the checkset function is in a global includes file. It is this part of development that I hate, but it is necessary. On other forms I have made, I created the form itself in a function so I can call it again and display the proper error messages and highlight the fields that errors were found in. This particular form, it shows again when it is submitted. Others I have created will show a "Thank you... the following information has been submitted" type message without the form.. hence the function to call the form. Hope this helps you some. Nate Quote Link to comment Share on other sites More sharing options...
garry Posted June 1, 2008 Author Share Posted June 1, 2008 Hmm.. I don't understand exactly how you are reloading the page with the same data.. Sorry - I'm sorta new at this! I already validate the information on the same page as the form. Here's a general idea of how I do it: if (isset($_POST['submitted2'])) { $something = $_POST['something']; // ect... } else { <form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF']; ?>" method="POST"> <table width="615" border="0"> <tr> <td><div align="right">Artist: </div></td> <td><input type="text" name="artist" value="<?php echo $artist; ?>"></td> <td><input type="hidden" name="artistid" value="<?php echo $artistid; ?>"></td> </tr> <tr> <td><div align="right">Description: </div></td> <td colspan="2"><textarea name="description" rows="20" cols="80"><?php echo $description; ?></textarea></td> </tr> <tr> <td><div align="right">Genre: </div></td> <td><select name="genreid" > <? $query = "SELECT * FROM genres "; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { echo "<option name=\"genre\" value=\"" . $row['id'] . "\" "; if ($genre_id == $row['id']){ echo "selected";} echo ">" . $row['genre'] . "</option><br />"; } ?> </select></td> <td></td> </tr> <tr> <td width="96" height="32"><div align="right">Artist Image:</div></td> <td width="298"><input name="image" type="file" /></td> <td width="486"><div class="message">Note: Must be at least 300x300. Leave blank if you don't want to change.</div></td> </tr> <tr> <td height="33"><input name="submitted2" type="hidden" value="1"></td> <td><input name="submit" type="submit" value="Edit" /></td> <td></td> </tr> </table> </form> } That is my actual form in the second part but I didn't put the whole "submitted2" part because it's very long! Can you explain to me exactly how you reload the form with the same user supplied data? Sorry for not understanding! Quote Link to comment Share on other sites More sharing options...
chronister Posted June 1, 2008 Share Posted June 1, 2008 In my example the page submits to itself. The include files do the work of processing the data before the form shows up. When the page loads after I have submitted the data it either shows a success message and the form is blank, or it shows an error message with the form filled out. That example was not the best one to show you... Check this one out, it is a contact form I created. It is a bit lengthy... so sorry to all who reads it... but I wanted to keep it mostly intact so you can see how I use the contact form function to call the form if an error is found. <?php // create a function containing the contact form so we can call it easily function contact_form() { function errorCheck($field) { if(isset($field) && $field != '') { echo 'value="'.$field.'"'; } } global $error,$state_list, $fname, $lname, $email, $address, $city,$state, $zip, $phone, $comments ; if(isset($error)){echo '<div class="error" align="center">'.$error.'</div>';}; ?> <table width="100%" border="0"> <tr> <td valign="top" nowrap><p> </p> <p align="center"><strong>What we can do to make your experience better? </strong></p> <p align="center">To contact a particular store, choose your the state, city and store from the dropdowns<br />otherwise leave them blank. </p> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" name="contentform" id="contentform"> <table border="0" align="center" cellpadding="4" cellspacing="0"> <tr> <td colspan="2" align="center"><?php @chainedselect(); ?></td> </tr> <tr> <td colspan="2" align="center" >Fields with an * are required</td> </tr> <tr valign="middle"> <td align="right" nowrap="nowrap"><label for="fname">*First Name:</label></td> <td><input class="textbox" name="fname" type="text" id="fname" <?php errorCheck($fname); ?> size="40" /></td> </tr> <tr valign="middle"> <td align="right" nowrap="nowrap"><label for="lname">*Last Name:</label></td> <td><input class="textbox" name="lname" type="text" id="lname" <?php errorCheck($lname); ?> size="40" /></td> </tr> <tr valign="middle"> <td align="right" nowrap="nowrap" ><label for="email">*E-mail Address:</label></td> <td><input class="textbox" name="email" type="text" id="email" <?php errorCheck($email); ?> size="40" /></td> </tr> <tr valign="middle"> <td align="right" nowrap="nowrap"><label for="address">Street Address:</label></td> <td><input class="textbox" name="address" type="text" id="address" <?php errorCheck($address); ?> size="40" /></td> </tr> <tr valign="middle"> <td align="right" nowrap="nowrap"><label for="city">City:</label></td> <td><input class="textbox" name="city" type="text" <?php errorCheck($city); ?> id="city" size="40" /></td> </tr> <tr valign="middle"> <td align="right" nowrap="nowrap"><label for="state">State:</label></td> <td> <select name="state" id="state" class="textbox" > <option selected>Choose State </option> <?php foreach($state_list as $k=>$v) { if(isset($error) && $state==$k) { echo '<option value="'.$k.'" selected="selected">'.$v.'</option>'; } else { echo '<option value="'.$k.'">'.$v.'</option>'; } } ?> </select> </td> </tr> <tr valign="middle"> <td align="right" nowrap="nowrap"><label for="zip">Zip Code:</label></td> <td><input class="textbox" name="zip" <?php errorCheck($zip); ?> type="text" id="zip" size="10" maxlength="5" /> </td> </tr> <tr valign="middle"> <td align="right" nowrap="nowrap" ><label for="phone">Phone:</label></td> <td><input class="textbox" name="phone" type="text" <?php errorCheck($phone); ?> id="phone" size="15" /> </td> </tr> <tr valign="top"> <td style="text-align: left; " colspan="2"><label for="comments">Questions, Praise or Other Comments*:</label></td> </tr> <tr valign="top"> <td> </td> <td ><textarea class="textbox" name="comments" id="comments" cols="40" rows="5" wrap="virtual" style="width: 90%;"><?php if(isset($error)){echo $comments;} ?></textarea> </td> </tr> <tr> <td colspan="2" align="center" ><input name="contact_form" type="submit" class="formbutton" id="contact_form" value="Submit" /> </td> </tr> </table> </form> <p> </p></td> </tr> </table> <?php } /*********************************************************** form function ends here & the real page display starts ************************************************************/ // if the form has not been submitted if(!$_POST['contact_form']) { // call the contact form function contact_form(); } else // else, the form has been submitted so we start processing it { // set our form fields as nice neat variables while trimming off white space $fname=ucfirst(trim($_POST['fname'])); $lname=ucfirst(trim($_POST['lname'])); $email=trim($_POST['email']); $address=trim($_POST['address']); $city=ucfirst(trim($_POST['city'])); $state=$_POST['state']; $zip=trim($_POST['zip']); $phone=trim($_POST['phone']); $comments=trim($_POST['comments']); @$store_id=$_POST['store_id']; // set the email address that this form will get mailed to $contact_email='Email To Send To<someone@domain.com>'; // begin checking for errors // if first name, last name, email address or comments is blank, set an error message if(strlen($fname) < 1 || strlen($lname) < 1 || strlen($email) < 1 || strlen($comments) < 1 ) { $error='Please fill in your First & Last name, your Email Address and a Question or Comment'; } if(isset($error)) // if an error is present { contact_form(); //call the form again, which is set up to receive the data that was submitted } else //or there is no error so send an email and display the success page { $to = $contact_email; // set up our mail headers $headers ="MIME-Version: 1.0" . "\r\n"; $headers .= "X-Mailer: PHP/" .phpversion() ."\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'Return-Path: '.$email.''."\r\n"; // set the return path as the guests email address $headers .= "From: $fname $lname <$email>"; // set the from as the guests email address $subject = 'Guest Comments'; // set the subject as Guest Comments if(mail($to, $subject, $body, $headers)) { ?> <br><br><h2 align="center">Thank You, The following information has been sent</h2> <table width="75%" border="0" align="center" cellpadding="4" cellspacing="0"> <tr> <td width="35%"> </td> <td width="65%"><?php echo $fname ?> <?php echo $lname ?></td> </tr> <tr> <td> </td> <td><?php echo $address ?></td> </tr> <tr> <td> </td> <td><?php echo $city ?>, <?php echo $state ?> <?php echo $zip ?> </td> </tr> <tr> <td> </td> <td><?php echo $phone ?></td> </tr> <tr> <td> </td> <td><?php echo $email ?></td> </tr> <tr> <td colspan="2" style="padding-left:50px"><?php echo $comments ?></td> </tr> </table> <?php } else { //debug the script here } } } ?> With the form in a function and some global vars called, I can call the form when needed and if an error is present, it runs through the checkset / errorcheck (same function as before, but a different name) function to pre-fill the form for the user. One of the things I don't have in the script is the state_list which is an array of states like so... $state_list = array( 'AL'=>'Alabama', 'AR'=>'Arkansas', etc..... ) Hope this clears things up for ya a bit. Sorry for using the previous example as it was not the best... its almost 4am here.. so please forgive My pillow is calling me. I also noticed that I probably should not create a function inside the function like I did as I can put it in the include file as I did for the other site. The code I posted is from 2 different sites. The contact form is for my job's new website I am developing so that's why I went the extra mile on it but not the other one which is for my use only. Nate Quote Link to comment 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.