0

I'm trying to enter a condition once a file is selected without having to use a submit button, here is my html :

<form id="fupload" method="post" enctype="multipart/form-data">Image : <input type="file" name="image"/></form>

I tried this :

if (isset($_POST['image'])){

echo '<div>Hello succes</div>';
}

not working and I also tried this :

if (isset($_FILES['image'])){
echo '<div>Hello succes</div>';
}

and also this :

if (!empty($_POST['image'])){
echo '<div>Hello succes</div>';
}

None of them worked, thank you for your help.

5
  • 3
    It is better to use javascript for your porpose
    – CIRCLE
    Oct 19, 2013 at 13:13
  • 4
    Your form is missing enctype="multipart/form-data" Oct 19, 2013 at 13:14
  • Yes, but my purpose isn't realy to show a "Hello success" but rather set a session variable. Oct 19, 2013 at 13:14
  • Javascript is your answer. Tap into the file input and send an ajax request to set a session variable.
    – Insyte
    Oct 19, 2013 at 13:45

2 Answers 2

1

Add enctype="multipart/form-data" to your form tag and add a submit button to the form.

Also you'd want to check if the file is actually uploaded and if there is any error and not just check if the "image" parameter is passed the in the POST.

if(empty($_FILES['image']['error']) && $_FILES['image']['tmp_name']){

   echo '<div>Hello success</div>';

}
2
  • already tried that, but when I use a submit button and click on it, the page is closed because the form is placed on a modal window. Oct 19, 2013 at 13:25
  • To prevent the modal window from closing, you'll have to switch to ajax-like upload and use JavaScript. You can use XMLHttpRequest for HTML5 browsers or you can submit the form to a hidden iframe by setting the target attribute to the id of the iframe in the form tag.
    – Ben Wong
    Oct 19, 2013 at 13:51
0

Thy the following:

if ($_FILES['image']['name']!=''){
    echo '<div>Hello succes</div>';
}
1
  • not working, the problem is that once the page is loaded, the page isn't refreshed when the file is selected, I though it was a similar issue to isset with submit buttons but it doesn't seems to work like that Oct 19, 2013 at 13:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.