0

I have this little form that has a button loading a little icon onto the form itself. The button says 'browse', and there is another button says 'upload image'. I find it kinda unnecessary to have two buttons to upload the image.

How can I make the upload happen on the end of Browse ?

here's the code by the way. (The upload button just activate the js that calls a php file that does the trick, the js function updates it on the form)

<form class="jQ-form" action="includes/ajaxupload.php" method="post" name="standard_use" id="standard_use" enctype="multipart/form-data">
    <fieldset>  
        <button id="image_upload_button" style="float:left;"  onclick= "$('#upload_area').css('display','none');
        $('#upload_area').fadeIn('slow');ajaxUpload(this.form,'includes/ajaxupload.php?filename=filename&amp;maxSize=200000&amp
        ... more func data.'); return false;" disabled>upload icon</button>

        <input type="file" id="upload_file" name="filename" style="float:left;width:70%;" size="42"/>

        <p style="float:right;color:#a2983c;margin:10px;width:373px;">
        Pick a nice icon that is max 300x300 pixels please
        </p>

        <?php 
            echo '<div id="upload_area" style="'.((($theiconname!='') && (file_exists($thumb_path.$theiconname))) ? '' : 'display:none;').'float:left;
            width:50px;height:50px;border:3px solid #000;margin-top:12px;margin-left:3px;">';
            if ($theiconname){

                if (file_exists($thumb_path.$theiconname))
                {
                    echo'<img id="the_logo" src="'.$thumb_path.$theiconname.'"/>';
                }

            }
        ?>
        </div>
    </fieldset>
</form>

Thanks in advance!

1 Answer 1

1

Sure! Just detect when the file input field has been changed, then submit the parent form. I notice you're using jQuery so I'll provide an answer using that:

$("#upload_file").change( function(){
     $("#standard_use").submit();
});

Alternatively, if you're submitting this via AJAX and not actually intending to submit the form, then replace $("#standard_use").submit(); with whatever function would normally fire when someone pressed your "upload image" button.

2
  • This is weird. I mark you as solved for the solution, but I am still having problems. the function ajaxUpload(this.form,'includes/ajaxupload.php?filename=filename&amp;... is supposed to send GET vars to the php, yet my DIV in the form is filling with errors stating that the page doesn't know all the variables it actually was sent. would you know how come ? Commented Sep 1, 2011 at 0:20
  • Thanks man ! SOLVED. I suspected the coding of &lt; and &gt; etc. in the variables the function is sending. I changed them all to the corresponding vars that is, &lt; got '<', &gt; got '>' etc. it works like a charm, again thanks ! Commented Sep 1, 2011 at 0:25

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.