[英]PHP - form checkbox submit with ajax
我已经设置了AJAX脚本,当您单击该按钮时,它将调用PHP文档。
现在的问题是,PHP文件的某些变量设置为常量,现在我希望它们以单击按钮时发送的内容为条件。
我希望有一个复选框,如果单击该复选框,会将PHP变量设置为TRUE,而如果未单击,则设置为FALSE。
我希望这个问题很清楚。
现在,我将在下面粘贴代码,因为它可能有助于澄清问题。
抱歉,我必须粘贴此“巨大”代码块,但我不确定哪个可能有用,哪些没有。
这是index.html
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","roulette.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Welcome to Trufa’s Roulette</h2></div>
<button type="button" onclick="loadXMLDoc()">Spin the Wheel!</button>
</body>
</html>
这是roulette.php的一部分
### Comprare pick to result
//The squares
$sqr_00 = -1;
$sqr_0 = 0;
$sqr_1 = 1;
//Placed bets, if true he placed bet
$bet_straight_placed = array(
$sqr_00 => false,
$sqr_0 => true,
$sqr_1 => true
);
正如我之前所说,我需要用$sqr_00 => false
替换FALSE, $sqr_00 => false
在选中复选框时为true的变量!
我希望这个问题很清楚可以理解,但是请要求任何澄清!
提前致谢!
特鲁法
编辑:
在@grossvogel答复中,我发布了我对他的回答的理解,因为它不起作用。 对不起,非常感谢!
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","roulette.php",true);
xmlhttp.send();
}
var isChecked = document.myform.checkbox.checked ? 1 : 0;
xmlhttp.open("GET","roulette.php?boxchecked=" + isChecked,true);
</script>
</head>
<body>
<div id="myDiv"><h2>Welcome to Trufa’s Roulette</h2></div>
<form name="myform">
<input type="checkbox" name="checkbox" value="checkbox" />00<br />
<button type="button" onclick="loadXMLDoc()">Spin the Wheel!</button>
</form>
</body>
</html>
和PHP
//The squares
$sqr_00 = -1;
$sqr_0 = 0;
$sqr_1 = 1;
//Placed bets, if true he placed bet
$chb_00 = $_GET['boxchecked'];
$bet_straight_placed = array(
$sqr_00 => (bool) $chb_00,
$sqr_0 => true,
$sqr_1 => true
);
//tell the user what number he piecked
echo "You chose this numbers: " . join(", ", array_keys(array_filter($bet_straight_placed))) . '<br />' . '<br />';
对不起,我知道这必须完全是基本的,但是我仍无法解决这个AJAX“问题”。
谢谢!!!
在您的JavaScript中,您可以执行以下操作
var isChecked = document.myform.checkbox.checked ? 1 : 0;
xmlhttp.open("GET","roulette.php?boxchecked=" + isChecked,true);
然后,在您的php中,您可以读取$_GET['boxchecked']
,如果选中了该框,则为1,否则为0。
编辑:将该想法扩展到多个领域。
为此,您需要添加任意数量的复选框,例如它们的名称为value1, value2, value3, ...
然后,您可以执行以下操作:
var value1 = document.myform.value1.checked ? 1 : 0;
var value2 = document.myform.value2.checked ? 1 : 0;
var value3 = document.myform.value3.checked ? 1 : 0;
...
和
xmlhttp.open("GET","roulette.php?value1=" + value1 + "&value2=" + value2 + "&value3=" + value3 ...,true);
在php中,读取$_GET['value1'], $_GET['value2'], $_GET['value3']...
了解概念后,可以根据需要编写一些函数来减少重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.