Reputation: 260
I am really happy with the priceless suggestions that you all gave me last time.i really thank you all. But this time i am trying to upload an file using ajax. As the user select the file in the browse window, it straight away uploads the image. Please help me with this too. Thank you all.
This is my code:
function upd(str)
{
$.ajax({
type:'post',
url:'upld.php',
datatype:'php',
data:'fname='+str,
success:function(responce)
{
alert("Uploaded");
}
});
}
</script>
</head>
<body>
<form action="upd.php" method="post" enctype="multipart/form-data">
<input type="file" name="namefile" id="namefile" onSelect="upd(this.value)">
</form>
and my upld.php contains the following code:
<?php
$target="uploads/";
$target=$target.basename($_FILES['file']["name"]);
move_uploaded_file($_FILES['file']["tmp_name"],$target);
echo basename($_FILES["file"]["name"])."File Uploaded";
?>
Upvotes: 0
Views: 1919
Reputation: 7073
You can use: $("form:first").submit();
On Change event can be attached against file object through jquery.
Otherwise, it may not be straight forward as you want. However, I have used Uploadify Jquery plugin that helps to do the following:
http://www.uploadify.com/about/
Upvotes: 2