PHP - 使用ajax表单复选框提交

时间:2022-11-24 14:50:43

I hava set up AJAX script that when you click the button it calls a PHP document.

我hava设置了AJAX脚本,当你点击它按钮它会调用一个PHP文档。

The problem I have is now some of the variables of the PHP file are set to constant, and now I want them to be conditional to what gets sent when the button is clicked.

我的问题是现在PHP文件的一些变量被设置为常量,现在我希望它们是在单击按钮时发送的内容的条件。

I want there to be a check box, that if clicked, will set a PHP variable to be TRUE, while if not click, is set to be FALSE.

我希望有一个复选框,如果单击,将设置一个PHP变量为TRUE,如果不是单击,则设置为FALSE。

I hope the problem is clear enough.

我希望问题很清楚。

I will now paste the code below because it may help to clarify the problem.

我现在将粘贴下面的代码,因为它可能有助于澄清问题。

Sorry I have to paste this "huge" chunk of code but I´m not sure which may be useful and which not.

对不起,我必须粘贴这个“巨大”的代码块,但我不确定哪些可能有用,哪些不是。

Here is index.html

这是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&rsquo;s Roulette</h2></div>
<button type="button" onclick="loadXMLDoc()">Spin the Wheel!</button>

</body>
</html>

And here is part of roulette.php

这是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
);

What I would need as I told before, is to replace the FALSE in $sqr_00 => false, for a variable that is true when the checkbox is checked!

正如我之前所说,我需要的是在$ sqr_00 => false中替换FALSE,对于在选中复选框时为true的变量!

I hope the question is clear enough to be understood but PLEASE ask for any clarifications needed!

我希望这个问题很清楚,但要请求任何澄清!

Thanks in advance!

提前致谢!

Trufa

特鲁法

EDIT:

编辑:

Addressing @grossvogel reply I am posting what I understood of his answer becuse it is not working. Sorry and thank you very much!

解决@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&rsquo;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>

and the php

和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 />';

I´m sorry I know this must be totally basic but I cant get my head around this AJAX "thing" yet.

对不起,我知道这一定是完全基本的但是我无法理解这个AJAX“的事情”。

Thank you!!!

谢谢!!!

1 个解决方案

#1


1  

in your javascript, you can do something like this

在你的javascript中,你可以做这样的事情

var isChecked = document.myform.checkbox.checked ? 1 : 0;
xmlhttp.open("GET","roulette.php?boxchecked=" + isChecked,true);

Then, in your php, you can read $_GET['boxchecked'], which will be 1 if the box was checked, 0 if it wasn't.

然后,在你的php中,你可以读取$ _GET ['boxchecked'],如果选中该框则为1,如果不是则为0。

EDIT: Extending the idea to more than one field.
To do this, you'd add as many checkboxes as you want, say they're names are value1, value2, value3, .... Then you can do something like this:

编辑:将想法扩展到多个领域。要做到这一点,你可以添加任意多个复选框,比如说它们的名字是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;
...

and

xmlhttp.open("GET","roulette.php?value1=" + value1 + "&value2=" + value2 + "&value3=" + value3 ...,true);

In php, read $_GET['value1'], $_GET['value2'], $_GET['value3']...

在php中,读取$ _GET ['value1'],$ _GET ['value2'],$ _GET ['value3'] ...

Once you get the concepts, you can write some functions to reduce the duplication, if you want.

获得概念后,如果需要,可以编写一些函数来减少重复。

#1


1  

in your javascript, you can do something like this

在你的javascript中,你可以做这样的事情

var isChecked = document.myform.checkbox.checked ? 1 : 0;
xmlhttp.open("GET","roulette.php?boxchecked=" + isChecked,true);

Then, in your php, you can read $_GET['boxchecked'], which will be 1 if the box was checked, 0 if it wasn't.

然后,在你的php中,你可以读取$ _GET ['boxchecked'],如果选中该框则为1,如果不是则为0。

EDIT: Extending the idea to more than one field.
To do this, you'd add as many checkboxes as you want, say they're names are value1, value2, value3, .... Then you can do something like this:

编辑:将想法扩展到多个领域。要做到这一点,你可以添加任意多个复选框,比如说它们的名字是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;
...

and

xmlhttp.open("GET","roulette.php?value1=" + value1 + "&value2=" + value2 + "&value3=" + value3 ...,true);

In php, read $_GET['value1'], $_GET['value2'], $_GET['value3']...

在php中,读取$ _GET ['value1'],$ _GET ['value2'],$ _GET ['value3'] ...

Once you get the concepts, you can write some functions to reduce the duplication, if you want.

获得概念后,如果需要,可以编写一些函数来减少重复。