2014-11-05 72 views
0

我有問題,使用php/ajax/mysql傳遞2個參數。PHP Ajax GET方法不會觸發

不能追蹤出了問題

這裏是Ajax代碼

<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> 
</script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
$('#myonoffswitch').click(function(){ 
var myonoffswitch=$('#myonoffswitch').val(); 
if ($("#myonoffswitch:checked").length == 0) 
{ 
var a=myonoffswitch; 
} 
else 
{ 
var a="off"; 
} 

$.ajax({ 
type: "POST", 
url: "process.php", 
data: "ps="+a , 
success: function(html){ 
$("#display").html(html).show(); 
} 
}); 

}); 
}); 
</script> 

<script type="text/javascript"> 
$(document).ready(function(){ 
$(".cb-enable").click(function(){ 
var parent = $(this).parents('.switch'); 
$('.cb-disable',parent).removeClass('selected'); 
$(this).addClass('selected'); 
$('.checkbox',parent).attr('checked', true); 
}); 
$(".cb-disable").click(function(){ 
var parent = $(this).parents('.switch'); 
$('.cb-enable',parent).removeClass('selected'); 
$(this).addClass('selected'); 
$('.checkbox',parent).attr('checked', false); 
}); 
}); 
</script> 

PHP:

$k=1; 
while($row = mysqli_fetch_array($result)) { 
$id = $row['ID']; 
$ps = $row['Status']; 

    echo "<div class=\"onoffswitch\">\n"; 
     echo "<input type=\"checkbox\" name=\"$k\" class=\"onoffswitch-checkbox\" id=\"$k\"\n"; 
     if($ps=="1") { 
      echo "checked "; 
     } 
     echo ">"; 
     echo "<label class=\"onoffswitch-label\" for=\"$k\">\n"; 
     echo " <div class=\"onoffswitch-inner\"></div>\n"; 
     echo " <div class=\"onoffswitch-switch\"></div>\n"; 
     echo "</label>\n"; 
    echo "</div>\n"; 
    $k++; 
} 

過程.PHP是類似於下面:

$ PS = $ _POS [ 'PS']; $ id = $ _ POS ['id'];

//的mysql_query( 「UPDATE TBL設置狀態= '$ PS',其中ID = $ ID」); //不起作用

的mysql_query( 「UPDATE TBL設置狀態= '$ PS',其中ID = 1」);

代碼更新,並僅使1參數/參數時的工作。

POST或GET方法都會好的。請幫忙嗎?

+1

編輯你的問題,而不是寫評論 – Sal00m 2014-11-05 08:02:38

+0

請確保你的代碼是正確縮進,否則它只是不可讀 – 2014-11-05 08:06:19

+0

更新代碼 – 2014-11-05 08:09:03

回答

0

裏面你的PHP文件你有很多的echo聲明。

將您的代碼更改爲此。並告訴我,如果它的工作原理

$k = 1; 

$content = null; 

while ($row = mysqli_fetch_array($result) or die ("<p>DB Erroe!!!</p>")) { 
    $id = $row['ID']; 
    $ps = $row['Status']; 

    $content .= "<div class=\"onoffswitch\">\n"; 
    $content .= "<input type=\"checkbox\" name=\"$k\" class=\"onoffswitch-checkbox\" id=\"$k\"\n"; 
    if ($ps == "1") { 
     echo "checked "; 
    } 
    $content .= ">"; 
    $content .= "<label class=\"onoffswitch-label\" for=\"$k\">\n"; 
    $content .= " <div class=\"onoffswitch-inner\"></div>\n"; 
    $content .= " <div class=\"onoffswitch-switch\"></div>\n"; 
    $content .= "</label>\n"; 
    $content .= "</div>\n"; 
    $k++; 
} 

echo $content; 
+0

它不能解決問題。加上我得到數據庫錯誤(DB Erroe !!!) – 2014-11-05 08:37:51

+0

所以這意味着你沒有結果返回'mysqli_fetch_array($ result)'...首先檢查查詢是否正確,它正在執行 – Umair 2014-11-05 08:47:12

+0

對不起,我的意思是點擊任何複選框沒有任何反應。數據庫不會影響任何行。 – 2014-11-05 08:49:18

0
var send='{"ps":"'+a+'","id":"'+id+'"}'; 
var obj=JSON.parse(send); 
$.ajax({ 
type: "GET", 
url: "process.php", 
data: obj, 
success: function(html){ 
$("#display").html(html).show(); 
} 
}); 
+0

嘗試過,但不起作用。即使我嘗試傳遞只有1個參數使用數據:「ps =」+ a – 2014-11-05 08:20:08

+0

什麼是你在控制檯中得到的錯誤..因爲我認爲你應該使用JSON發送數據..告訴我錯誤在colsole ..我會給你json方法 – 2014-11-05 08:21:21

+0

雖然沒有錯誤。什麼都沒有發生 – 2014-11-05 08:22:22

0

不要,我再說一遍,不這樣做,使用mysql_ *接口。切換到mysqli或PDO。它在最新的PHP版本中已被棄用。而且特別是不要混合它們!它不會工作!