Jump to content

No lost form data


Recommended Posts

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

 

<?php
session_start ();

//check if its not logged or its admin redirect to login page
include ("scheck.php");


//Connect to db
include ("conn.php");

//Select db
include ("select.php");

//header codes
include ("headercodes.php");

//PAGE DISPLAY
//******************* PAGE CODES HERE


foreach ($_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 HERE
include ("footercodes.php");

}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.