Home Ask Login Register

Developers Planet

Your answer is one click away!

AW3 February 2016

Resetting check box value when 'back' is hit

I have a site that allows user to upload files to a folder. First I check if the file exists, if it does then I check the value of a checkbox to determine if the user wants to overwrite the existing file. If upload is hit and the box is unchecked, I do a die() that has a message and a back button. However, when I hit back, check the checkbox (the file is still displayed as if it was selected), then submit again, it doesn't overwrite the file (but appears to, no die). Is the checkbox value not being changed or reset when I hit back or something?

Maybe worth mentioning, there is a form (if method=post) and a form for (else) but they are exactly identical.

Can someone help me out please? Thanks.

Here is my code:

Form:

<form id='upload' method='post' enctype='multipart/form-data' action='<?php $_server['PHP_SELF']; ?>'>
      <input type='file' name='file_upload'>
      <br><br>
      <label><input type="checkbox" id="overwrite" name="overwrite" value="Yes"> Overwrite file (if file exists)</label>
      <br><br>
      <input type='submit' value="Upload">
      </form>

Rest:

if(file_exists($fullFileName)){ 
        $confirmation = $_POST["overwrite"];
        if($confirmation == "Yes") {        
            unlink($fullFileName); //overwrite confirmed, unlink old file
        }
        else {
            die("Upload aborted. Please check 'Overwrite file (if file exists)' box to overwrite existing file&nbsp;&nbsp;<input type='submit'  href='#' onclick='history.back();' value='Back'>");
        }
    }


if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], '' . $_FILES['file_upload']['name'])){ //move new file into place
        die('Errrrrr! Error uploading file - check destination is writeable.');
    }

Answers


Source Matters February 2016

Instead of a die(), you can just do a PHP header redirect which would essentially "clear the form" and is would be a new page load. If you wanted to retain the selected file, you could pass it back as a GET value.

Post Status

Asked in February 2016
Viewed 3,568 times
Voted 4
Answered 1 times

Search




Leave an answer


Quote of the day: live life