0

I want the start_uploading function only to start if all the conditions are met and the file is uploading, right now the 'loading' message pops up for a split second when the form is submitted but it did not meet all the conditions to upload.

if(condtion){// don't start function} else

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
  //now start function} else echo "error";

javascript

function start_Uploading(){
  document.getElementById('message').innerHTML = 'Loading... <br> Please Wait';
  return true;
}

and html

<form action="something.php" method="POST" enctype="multipart/form-data"
 onsubmit="start_Uploading();">    
1
  • What will you do when Javascript is disabled in the browser? Commented Jan 13, 2013 at 0:37

2 Answers 2

2

Since you are checking for the condition to be true in php, I would simply only echo out the js method name if the condition is met.

Like so:

<?php
  if(true) $onsubmit = "start_Uploading();"; // Put actual condition in parantheses
  else $onsubmit = "";
?>

And then in your form:

<form action="something.php" method="POST" enctype="multipart/form-data"
onsubmit="<?php echo $onsubmit; ?>">

If the condition isn't met, no method name will be provided and js simply won't know what to look for on submit.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the code, I tried it but it only seems to be effective on form re-submission
Please have a look at the pages sourcecode in the browser - does the method name appear if the condition is met? If so, there may still be an error in your js - if not, please tell us specifically what else you can find out.
I figured it out now, it only recognizes the variable after uploading by which time its to late to be used in the html, violating cause and effect. I'll have to figure out a different tactic, Thanks for the code though
1

move_uploaded_file() is copying the uploaded file from apache's temp directory to user directory, so when you submit the file, so it would be already uploaded to the temp directory before the php side can start processing that file.

So, I recommend you to use a client side (javascript) form validation, or make a research about AJAX file uploads and/or form validation. PHP is a server side language, and runs before a page loads. Not inside a page. (Unless it's an AJAX call from client.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.