I am using this AJAX Image Uploader script and what I am trying to do is pass a hidden variable.
<input type="hidden" name="test" value="test" />
But since there is no real submit button, it is all 开发者_JAVA技巧AJAX based, I cannot just call $_POST['test']
;
Part of the jQuery is:
if (formdata) {
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
It should probably go somewhere in there but I'm not sure on how to approach this. If anyone has any suggestions I would really appreciate it.
Thank you!
Try this:
Just give your hidden input an id so we can refer to it and get the value. I used hidden_input
in this case.
if (formdata) {
//New line is below!
formdata.append("test",$('#hidden_input').val());
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
Then your upload.php
should be able to access the value with $_POST['test']
But since there is no real submit button, it is all AJAX based, I cannot just call $_POST['test'];
Sure you can. AJAX can send any header you want in the type
parameter. If you put type:'post'
just as you have it now, the field with name="test"
will be available via $_POST['test']
You can have any event trigger the ajax, including the page load.
$('somenode').bind('someevent',function(){
So, say you want the ajax to post every time a field value is changed:
$('input').change(function(){
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
$('#response').html(res);
}
});
});
精彩评论