February 26, 200917 yr I have a long form to fill in on a .php page, and if you miss a field and press submit, when I go back all the form information is lost and I have to type it all again. On some sites I have noticed that when you hit the back button on your browser it saves the form information, how can i achieve this? Thanks in advance!
February 26, 200917 yr I belive it would be somthing along the lines of adding the $_POST var to the form field value. <input type="text" name="email" value="<?php echo $_POST['email']" /> Its been along time since I did anything like this. Im sure the $_POST works on going back but not 100%. Edited February 7, 201016 yr by Sam G added [code] tags
February 26, 200917 yr Also - Add some PHP validation i.e. if field is empty re echo the form and use the $_POST in the value area, you can then also highlight the missed or incorrect fields saving the user from pressing back in the first place.
February 26, 200917 yr Hi, Welcome to the forum. Look up session cookies and session variables in Google. Think that's how you do it?
February 26, 200917 yr Hi, Welcome to the forum. Look up session cookies and session variables in Google. Think that's how you do it? No need for sessions or variabled.... I'll post a form I just wrote and you can see what I do.... <?php $post = false; //not posted yet $display = 'block'; $red_border = "style='border: 1px solid red'"; $vCon = array(); $vCon['title'] = funcSubmitWasher('post','title','text',100); $vCon['first_name'] = funcSubmitWasher('post','first_name','text',100); $vCon['second_name'] = funcSubmitWasher('post','second_name','text',100); $vCon['email_address'] = funcSubmitWasher('post','email_address','text',100); $vCon['confirm_email'] = funcSubmitWasher('post','confirm_email','text',100); $vCon['phone_number'] = str_replace(' ','',funcSubmitWasher('post','phone_number','text',15));//remove spaces from phone number $vCon['fault_details'] = funcSubmitWasher('post','fault_details','text',1000); $vCon['date'] = date('l jS \of F Y h:i:s A'); $vCon['address'] = funcSubmitWasher('post','address','text',1000); $vCon['order_id'] = funcSubmitWasher('post','order_id','text',100); if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){ ///-------------------------------------] /// Post Action ///------------------------------------// $post = true; //validate $val = new validation; //initialise class $eArray = array(); if(!$val->validate(strtolower($vCon['title']),'check_value',0,100,'please select')){ $eArray[0] = true; } //title - 0 if(!$val->validate($vCon['first_name'])){ $eArray[1] = true; } //first name - 1 if(!$val->validate($vCon['second_name'])){ $eArray[2] = true; } //second_name - 2 if(!$val->validate($vCon['email_address'],'email')){ $eArray[3] = true; } //email - 3 if(!$val->validate(array($vCon['confirm_email'],$vCon['email_address']),'compare')){ $eArray[3] = true; } //email - 3 if(!$val->validate($vCon['fault_details'])){ $eArray[4] = true; } //fault details - 4 if(!$val->validate($vCon['address'],'strlen',1,1000)){ $eArray[5] = true; } //address - 5 if(!$val->validate($vCon['order_id'],'numeric',0,100)){ $eArray[6] = true; } //order id - 6 if(!empty($vCon['phone_number'])){ if(!$val->validate($vCon['phone_number'])){ $eArray[7] = true; } }//telephone number - 7 if(!$val->validate($vCon['order_id'],'check_database_for_id',0,100,'complete_orders')){ $eArray[6] = true; } ///------------------------------] // Call to action ///-----------------------------// if(isset($_FILES['photo'])){ if($_FILES['photo']['size'] > 0){ //upload image $file = array(); $file['file'] = $_FILES['photo']; $file['new_file_name'] = sha1(time().uniqid().uniqid()); $file['ext'] = substr(strrchr($file['file']['name'], '.') ,1); $file['output'] = $_SERVER['DOCUMENT_ROOT'].'/files/user_uploaded/'.$file['new_file_name'].'.'.$file['ext']; $file['allowed_exts'] = array('jpg','jpeg','gif','png'); if(in_array($file['ext'],$file['allowed_exts'])){ $image = new image; $image->load($file['file']['tmp_name']); $image->resizeToWidth(600); $image->save($file['output']); }else{ $eArray[9] = true; # JON - edit below string to change output message $file_message = "The photo you uploaded was not of a suitable format"; } } } //construct mail and log email if(count($eArray) == 0){ $vMsg = " Hi, The damaged / faulty goods form was filled out on ".RICE_HTTP_HOST." with the details below: Customer name: {$vCon['title']} {$vCon['first_name']} {$vCon['second_name']} Customer Email: {$vCon['email_address']} Customer phone: {$vCon['phone_number']} Fault Details: {$vCon['fault_details']} Order ID: {$vCon['order_id']} Address: ".nl2br($vCon['address'])." "; if(isset($file['new_file_name'])){ $vMsg .= "Photo: ".RICE_HTTP_HOST."/files/user_uploaded/".$file['new_file_name'].".".$file['ext']; } mail(RICE_EMAIL_RECIPIENT,'Damaged / Faulty Goods',$vMsg,"FROM: ".RICE_EMAIL_SENDER,"-f".RICE_EMAIL_RECIPIENT); $x = new site; $x->log_email($vMsg,RICE_EMAIL_RECIPIENT); } }//end request method if if( (isset($eArray)) && (count($eArray) > 0) ){ echo "<div class='warning'>The items with a red border require your attention"; if(isset($eArray[9])){ echo "<br />".$file_message; } echo "</div>"; } if( (isset($eArray)) && (count($eArray) < 1) ){ echo "<div class='success'>Your submission below has been received by our customer services team, who will endeavour to contact you as soon as possible with a solution."; echo "</div>"; $display = 'none'; } ?> <div id="form_top" style="display: <?php echo $display; ?>"></div> <div id="form" style="display: <?php echo $display; ?>"> <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" enctype="multipart/form-data"> <div class="form_padder"> <label for="title">Title:<span class="red">*</span></label> <div class="form_input"> <span <?php echo (isset($eArray[0])) ? "id='damaged_goods_title_wrap'" : ""; ?>> <select name="title" class="form_object"> <?php $site = new site; foreach($site->title as $titles){ if( ($post) && ($titles == $vCon['title']) ){ $selected = 'SELECTED'; }else{ $selected = ''; } echo "<option value='$titles' $selected>$titles</option>"; } ?> </select> </span> </div> </div> <div class="form_padder"> <label for="first_name">First name:<span class="red">*</span></label> <div class="form_input"> <input type="text" name="first_name" maxlength="100" value="<?php echo $vCon['first_name']; ?>" <?php if(isset($eArray[1])){ echo $red_border; } ?> /> </div> </div> <div class="form_padder"> <label for="second_name">Second name:<span class="red">*</span></label> <div class="form_input"> <input type="text" name="second_name" maxlength="100" value="<?php echo $vCon['second_name']; ?>" <?php if(isset($eArray[2])){ echo $red_border; } ?> /> </div> </div> <div class="form_padder"> <label for="email">Email address:<span class="red">*</span></label> <div class="form_input"> <input type="text" name="email_address" maxlength="100" value="<?php echo $vCon['email_address']; ?>" <?php if(isset($eArray[3])){ echo $red_border; } ?> /> </div> </div> <div class="form_padder"> <label for="email_confirm">Confirm email address<span class="red">*</span></label> <div class="form_input"> <input type="text" name="confirm_email" maxlength="100" value="<?php echo $vCon['confirm_email']; ?>" <?php if(isset($eArray[3])){ echo $red_border; } ?> /> </div> </div> <div class="form_padder"> <label for="phone_number">Phone number:</label> <div class="form_input"> <input type="text" name="phone_number" maxlength="100" value="<?php echo $vCon['phone_number']; ?>" <?php if(isset($eArray[7])){ echo $red_border; } ?> /> </div> </div> <div class="form_padder"> <label for="address">Postal address:<span class="red">*</span></label> <div class="form_input"> <textarea name="address" cols="32" rows="5" <?php if(isset($eArray[5])){ echo $red_border; } ?>><?php echo $vCon['address']; ?></textarea> </div> </div> <div class="form_padder"> <label for="fault_details">Fault details:<span class="red">*</span><br /><small>1000 characters maximumum</small></label> <div class="form_input"> <textarea cols="32" rows="5" name="fault_details" <?php if(isset($eArray[4])){ echo $red_border; } ?>><?php echo $vCon['fault_details']; ?></textarea> </div> </div> <div class="form_padder"> <label for="order_id">Order Number:<span class="red">*</span></label> <div class="form_input"> <input type="text" name="order_id" maxlength="100" value="<?php echo $vCon['order_id']; ?>" <?php if(isset($eArray[6])){ echo $red_border; } ?> /> </div> </div> <div class="form_padder"> <label for="photo">Photo:<small>(Gif, Jpeg, Jpg or Png format)</small></label> <div class="form_input"> <span style="width: 202px; padding-bottom: 7px;"> <span <?php echo (isset($eArray[9])) ? 'id="damaged_file_wrap"' : ''; ?>> <input type="file" name="photo" style="width: 146px" /> </span> </span> </div> </div> <div class="form_padder"> <label for="submit"> </label> <div class="form_input"> <input type="submit" name="submit" value="Send" /> </div> </div> </form> </div> <div id="form_bottom" style="display: <?php echo $display; ?>"></div>
Create an account or sign in to comment