我有使用php / ajax / mysql传递2个参数的问题。
无法找出问题
这是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 [&#39; PS&#39;]; $ ID = $ _ POS [&#39; ID&#39;];
// mysql_query(&#34; UPDATE Tbl set Status =&#39; $ ps&#39;其中ID = $ id&#34;); //不起作用
mysql_query(&#34; UPDATE Tbl set Status =&#39; $ ps&#39;其中ID = 1&#34;);
代码在仅传递1个参数/参数时更新并正常工作。
POST或GET方法就可以了。有什么帮助吗?
答案 0 :(得分: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;
答案 1 :(得分: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();
}
});
答案 2 :(得分:0)
不要,我再说一遍,不要使用mysql_ *接口。切换到mysqli或PDO。它已被弃用并在最新的PHP版本中消失。并且特别是不要混合它们!它不起作用!