How to pass image location to php function using jquery callback function?
<div class="hideAfterSuccess">
<label for="avatar">Upload an Avatar :</label>
<input type="file" name="avatar" id="avatar" />
</div>
and below is jquery, which should pass the image location to php function in another file (parsePortal.php). I can transfer other kinds of data to the file, but the image failed. I thought, since it is posting the image location string, I could use $ _FILES [$ _ POST ['image]] [' name '] and then upload the image. i tried internet, of course didn't find what i need.
$(function(){
var imageLoc = $('avatar').val();
var url = "parsePortal.php";
$.post(url, {
status : "getimage",
image : "imageLoc"
}, function(result){
});
});
0
source to share
2 answers
I did it successfully using jquery forms plugin. this means it sent the whole path from which I got the filename and printed it out. THANK YOU VIC, I couldn't do it without such help.
here is the javascript.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
// prepare the form when the DOM is ready
$(document).ready(function() {
// bind form using ajaxForm
$('#htmlForm').ajaxForm({
// target identifies the element(s) to update with the server response
target: '#htmlExampleTarget',
// success identifies the function to invoke when the server response
// has been received; here we apply a fade-in effect to the new content
success: function() {
$('#htmlExampleTarget').fadeIn('slow');
}
});
});
</script>
and here is the html
<form id="htmlForm" action="file.php" method="post">
Message: <input type="text" name="message" value="Hello HTML" /> <p></p>
Image : <input type="file" name="image" id="image" /><p></p>
<input type="submit" value="Echo as HTML" />
and php is here
$image = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
move_uploaded_file($image, "uploads/".$name);
echo '<div style="background-color:#ffa; padding:20px">' . $_POST['message'] . '</div>';
echo '<img src="uploads/'.$name.'"/>';
0
source to share