我hava設置AJAX腳本,當你點擊它調用一個PHP文檔的按鈕。PHP - 表單複選框提交與AJAX
我現在的問題是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
);
我需要什麼,我以前告訴過,是替代FALSE在$sqr_00 => false
,對於當複選框被選中時變量是真的!
我希望這個問題已經夠清楚了,但是請您澄清一下!
提前致謝!
特魯
編輯:
尋址@grossvogel回覆我張貼我的理解他的回答becuse它無法正常工作。對不起,非常感謝!
<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「東西」呢。
謝謝!!!
一個建議,當你試圖獲得AJAX的掛件:使用Firebug。它給出了有用的錯誤,讓你把你的javascript中的斷點,並讓你查看所有異步請求的內容。無價。 http://getfirebug.com/ – grossvogel 2010-10-19 22:54:29
好的建議,我會感謝你! – Trufa 2010-10-19 23:00:23