0

I have the following form:

    <form id="MainForm" enctype="multipart/form-data" method="post" action="index.php"> 
            <input type="file" name="file" id="file" class="upload" onchange="document.getElementById('MainForm').submit();" title="Go!" accept="image/*">
    </form>

And in the beginning of the same page, in the php:

 if(isset($_POST['file']))
 {     
     echo 'ok'; // and do stuff latter
 }

The idea is to send the file when I select it, without having to press any kind of submit button, but its not echoing ok. When I change my form method to get, and put $_GET instead of $_POST it echoes ok, but I need to do it with post and I couldn't figure out what I'm missing yet.

sorold
  • 475
  • 7
  • 17

2 Answers2

0

SORRY, I pasted something wrong in there without rechecking it - that made no sense. I edited it now, hopefully should make more sense...

if this if(isset($_POST['file']))is on the same page, it's referring to something that comes from a previous page, so posting something on that page and referring to it on the same page won't work

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • I'm actually handling all my requests like that, on the same page 'if(isset($_POST['submit ']' { do stuff } . Everything is working fine but this one. Am I handling all the posts wrongly? – sorold Dec 12 '15 at 03:02
0

try this,

   <?php  if($_FILES['file']['size']>0){     
 echo 'ok'; // and do stuff latter
 }else{
?>

 <form id="MainForm" enctype="multipart/form-data" method="post" action="#"> 
        <input type="file" name="file" id="file" class="upload" onchange="document.getElementById('MainForm').submit();" title="Go!" accept="image/*">
</form>
<?php
   }
?>
Billy
  • 2,448
  • 1
  • 10
  • 13