I'd like to process a form that passes a file and checks to see if the file exists, and if so, I'd like to throw a popup which asks the user if the file can be overwritten. So I'm just trying to use Javascript's confirm() function which passes a true/false back to PHP to update the fields. There's a catch 22 because what I'm basically trying to do is pass a variable between PHP and Javascript which I know I can't do. So here's the portion I've got when the user has passed the form over to be processed and checking the status of the file:

if (file_exists( $file_dir . '/' . $_FILES['file']['name'] )) {
echo "
<script>
if (!confirm('This file already exists...Overwrite File?')) {
location='FormRetrieve_TestIssues.php?id=$ID&sort_by=ID&sort_asc=0&formtype=UPDATE&kc_tbl=$kc_tbl';
}
</script>";
}

if ( $FILES['file']['name'] != "") {
move_uploaded_file( $
FILES['file']['tmp_name'], $file_dir . '/' . $_FILES['file']['name'] );

$file_name=$_FILES['file']['name'];
$sql = "update $kc_tbl SET file='$file_name' where ID=".$ID;
$sql_exec = odbc_exec( $cnx, $sql );
}

And I thought that if the user backs out of overwriting the file, that I would use Javascript to just redirect out of the current page and stop processing it and go back to the original form. But the rest of the PHP code ends up executing anyway and I realize the Javascript won't stop this from happening. Any way I can do this on the form page before submitting?

    Sorry I don't know anything about JAVA, but this might help...

    Maybe you don't quite want to do a pop-up window, especially since popup-blockers are becoming so popular. I do something similar with file deletion in a CMS I'm writing, but instead of using a popup, i decided to move the user to a confirmation page: "Are you sure you want to delete {filename}? [yes] [NO]"

    At least with that, they can back out ("NO" takes them back to the previous page). Clicking "yes" does the obvious then spits them back to wherever I instruct the page to leave them.

    Hope this helps.

      Originally posted by bgneuschafer
      Any way I can do this on the form page before submitting?

      If you plan on checking for the presence of a file only after your user has submitted the form, then you'll need to do an if/else statement.

      Pseudo-code:

      if ($some_file exists?) {
      echo '<script>
      // do JavaScript message
      </script>';
      } else {
      // do file upload/update;
      }

        Write a Reply...