2010-10-19 145 views
0

我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&rsquo;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&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> 

和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「東西」呢。

謝謝!!!

+0

一個建議,當你試圖獲得AJAX的掛件:使用Firebug。它給出了有用的錯誤,讓你把你的javascript中的斷點,並讓你查看所有異步請求的內容。無價。 http://getfirebug.com/ – grossvogel 2010-10-19 22:54:29

+0

好的建議,我會感謝你! – Trufa 2010-10-19 23:00:23

回答

1

在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']...

一旦你的理念,你可以寫一些功能,以減少重複,如果你想。

+0

謝謝,我會試試看! – Trufa 2010-10-19 22:01:50

+0

我應該在index.html中輸入: 00
? – Trufa 2010-10-19 22:10:42

+0

是的,您需要添加複選框輸入。它應該駐留在一個表單中(在我的例子中,'name =「myform」'),它的名字應該與javascript(在我的例子中,'checkbox')匹配。值並不重要,但如果您希望在加載頁面時檢查它,則可以添加checked =「checked」。 – grossvogel 2010-10-19 22:20:51