How to avoid data loss after server validation in php?

In my create page, i am using one or more file upload text box , select box ,text box inside a form , After filling all form datas and submit, I will do a server validation then insert into db... Before insert into db, if server validation fails, filled datas are lossed , Is there any easy way to prevent data loss?

for select and text box we can pass the value of $_POST

But for file upload text box , how to prevent data loss? Can we store in session?

Thanks in advance..

Upvotes: 0

Views: 1007

Answers (3)

Jithin
Jithin

Reputation: 2604

You can save the data in variables and populate it later, except for the type="file" input field. It will be a security problem if the files can be prepopulated(or say selected without user permission) and browsers won't allow it.

One option is to save the file in any temporary folder in server and if validations worked fine, move it to the uploads directory.

Another option is to show your form in steps(in pages) and the file upload option in the last step, so that the user will reach the file upload section after all validations and you can do the file upload validation in the last step.

Upvotes: 0

Ravi Kant Mishra
Ravi Kant Mishra

Reputation: 768

As per my knowledge you can not maintain the file upload values as like the other form elements.

If you still want to maintain the $_FILES values. You can take them in session.

$_SESSION['imagename'] = $_FILES['image']['name'];
$_SESSION['imagetmp']  = $_FILES['image']['tmp_name'];

Upvotes: 1

Prakash Chhetri
Prakash Chhetri

Reputation: 7

well you can use some variables to store the data, then you can perform the validation

well i am giving a example of php validation

<?php
  if(isset($_POST['submit'])){
    $name = $_POST['name'];  //
    // and some other data input
    and goes the validation part

?>

<form ... >
<input type="text" name="name" value="<?php if(isset($name)) echo $name; ?>"
</form>

well.. this must work.. you might find other better ways.. but this will also work

Upvotes: 0

Related Questions