如何将 jQuery 变量传递给 PHP 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5202070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass jQuery variables to PHP variable?
提问by geetha
How do you pass a variable from jQuery to PHP without a page refresh? When I click on a checkbox I would like to pass a variable from jQuery to PHP. I am also using formdialog.
如何在不刷新页面的情况下将变量从 jQuery 传递到 PHP?当我点击一个复选框时,我想将一个变量从 jQuery 传递到 PHP。我也在使用表单对话框。
My PHP code
我的 PHP 代码
<?php
echo "<input name='opendialog' type='checkbox' class='opendialog' onclick='countChecked()' value=".$taskid." ?>" /> </td>"
?>
my javascript code
我的 JavaScript 代码
function countChecked() {
var n = $("input:checked").length;
var allVals = [];
$('input:checkbox:checked').each(function() {
allVals.push($(this).val());
});
$('.sel').text(allVals+' ');
$('.select1').val(allVals);
alert(allVals);
<?php $taskidj=$rowtask['taskID'];
// echo "aaa...".$rowtask['taskID']; ?>
}
$(":checkbox").click(countChecked);
// my jquery code
$('.mydialog').dialog({
bgiframe: true,
autoOpen: false,
modal: true,
width: 700,
height:500,
resizable: false,
open: function(){closedialog = 1;$(document).bind('click', overlayclickclose);},
focus: function(){closedialog = 0;},
close: function(){$(document).unbind('click');},
buttons: {
Submit: function(){
var bValid = true;
// allFields.removeClass( "ui-state-error" );
// bValid = bValid && checkLength( name, "username", 3, 16 );
// bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
if ( bValid ) {
processDetails();
return false;
}
},
Cancel: function() {
$( this ).dialog( "close" );
$('input[name=opendialog]').attr('checked', false);
}
}
});
$('.opendialog').click(function() {
$('.mydialog').dialog('open');
closedialog = 0;
});
回答by Explosion Pills
Ajax can do this. Google it, and check out api.jquery.comand look at the ajax functions, .ajax(), .post(), .get(), .load(), etc.
Ajax 可以做到这一点。谷歌一下,查看api.jquery.com并查看 ajax 函数,.ajax() 、.post() 、.get() 、.load() 等。
As for your specific question, here is what you would do:
至于你的具体问题,这是你会做的:
//Javascript file
$("input[type=checkbox]").click(function () {
$.post('my_ajax_receiver.php', 'val=' + $(this).val(), function (response) {
alert(response);
});
});
//PHP file my_ajax_receiver.php
<?php
$value = $_POST['val'];
echo "I got your value! $value";
?>