danielwilliamsongle Posted June 7, 2013 Share Posted June 7, 2013 Hi all, I have an upload page on a website that works well, however if a user doesn´t meet the criteria i.e file size too big or empty field then an error message is returned. Once the user clicks back all the original correct data is lost. How can I make this data be saved even if there is an error? The current page code is below. Thanks in advance <?phpsession_start ();//check if its not logged or its admin redirect to login pageinclude ("scheck.php");//Connect to dbinclude ("conn.php");//Select dbinclude ("select.php");//header codesinclude ("headercodes.php");//PAGE DISPLAY//******************* PAGE CODES HEREforeach ($_POST as $field => $value) {if (!$value){$err .="<center><br><font color=#f69900><b>$field</b></font> is required<br>";}}if ($err){echo $err;echo"<br><br><a href='javascript:history.go(-1)'>Back</a>";include ("footercodes.php");exit;}else {//$file = trim(addslashes($_POST['file']));//$manufacturer = trim(addslashes($_POST['manufacturer']));//$listingdate = trim(addslashes($_POST['listingdate']));//$views = trim(addslashes($_POST['views']));//check if user create new name for group or use current names.//if ($_POST[imggroup]=="Create New Category")//{//$imggroup=$_POST[newproduct];//}//else//{//$imggroup=$_POST[imggroup];//} $listingdate= date('j F Y');$views=0;$username=$_SESSION ["valid_user"];$confirm=0; //file upload if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("uploads/products/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists change your file name. "; } else { $random_digit=rand(0000,9999); $_FILES["file"]["name"]=$random_digit.$_FILES["file"]["name"]; move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/products/" . $_FILES["file"]["name"]); echo "<center><br><b>Thank You!</b>"; $file=$_FILES["file"]["name"]; //Create columns and records in database with file $sql="INSERT INTO `products` (`category`,`county`,`name`,`condition`,`partno`,`price`,`file`,`manufacturer`,`listingdate`,`views`,`username`,`confirm`,`description`) VALUES ('$_POST[category]','$_POST[county]','$_POST[name]','$_POST[condition]','$_POST[partno]','$_POST[price]','$file','$_POST[manufacturer]','$listingdate','$views','$username','$confirm','$_POST[description]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<br>Your product has been uploaded successfully , as soon as admin confirms it , it will be listed</center><br/>"; } } } else { echo "You did not specify any image to upload or your image is not acceptable (Acceptable types : .JPG and .GIF) <br> Please make sure your image name does not have space or special characters"; } //******************* PAGE CODES ENDS HEREinclude ("footercodes.php");}?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 7, 2013 Share Posted June 7, 2013 There are ways to try and force the browser to remember the data in form fields when clicking the back button, but I'm too lazy to look it up and there is a much better way to solve this. When I have a form where validation is needed, I submit the form to itself and do the validation. If validation fails I display the error message AND display the form populating the fields with the previously submitted values. If validation passes, then include the page to process the submitted values. 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.