Shikhil Bhalla
Shikhil Bhalla

Reputation: 392

submit form automatically after select image from browse window while uploading image

I want to upload image and want to store that image in database as well.But when i click on image to open browse file dialogue box the form already submited.But i want to submit form after i select image.

enter image description here

Here is my code

<form>

<input type="file" id="my_file" style="display:none;" />
<input type="image" src="albert-einstein-bike.jpg" width="90px" height="200" />
</form>

 <script>

$("input[type='image']").click(function() {
$("input[id='my_file']").click();
});
</script>

Upvotes: 3

Views: 4813

Answers (1)

Nibin
Nibin

Reputation: 3960

Hope something like this might do it for you mate... :)

html

<div id='preview'></div>
    <form id="imageform" method="post" enctype="multipart/form-data" action='ajaximage.php'>
    <input type="file" name="photoimg" id="photoimg" />
     </form>

Script File

$('#photoimg').on('change', function() 
 {
      $("#imageform").ajaxForm({target: '#preview', //Shows the response image in the div named preview 
         success:function(){

         }, 
         error:function(){

          } 
       }).submit();
});

ajaximage.php

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
$tmp = $_FILES['photoimg']['tmp_name'];
$path = "uploads/";
move_uploaded_file($tmp, $path.$name) //Stores the image in the uploads folder
}

Upvotes: 4

Related Questions