有没有办法将多个参数传递给在php页面中回显的ajax成功函数?

时间:2014-05-20 05:31:18

标签: jquery ajax

这是我的代码:

edit_menu.php

function update_module(id) {
var check;
 if($("#checkbox1"+id).attr('checked')){
    check=1;
  }
 else{
    check=0;
 }

var name = $('.name_text_module'+id).val();
var displayorder = $('.desc_text_module'+id).val();

$.ajax ({
    type: "GET",
    url: "update_module.php",
    data: "name="+name+"&displayorder="+displayorder+"&id="+id+"&check="+check,

    success: function(msg)
        {
           //In this function I want to send two parameters to the success function
            // somewhat like this 'success: function(msg1,msg2)'
        }       
});      

}

update_module.php

session_start(); 
include('../../db.php');

$module=$_GET['name'];
$id=$_GET['id'];
$check=$_GET['check'];
$displayorder=$_GET['displayorder'];

$modulename = str_replace("_"," ",$module);

echo $modulename;
echo $displayorder;

HmsDB::getInstance()->updateModule($id,$modulename,$displayorder,$check);

echo "<script>alert('Updated Successfully');</script> ";

我必须发送变量$ modulename&amp; $ displayorder将success函数作为两个不同的变量。这可能吗 ? 非常感谢:)

1 个答案:

答案 0 :(得分:0)

总结一下,你可以这样做:

php文件

$data = array();
$displayorder=$_GET['displayorder'];
$modulename = str_replace("_"," ",$module);

$data['displayorder'] = $displayorder;
$data['modulename'] = $modulename;

HmsDB::getInstance()->updateModule($id,$modulename,$displayorder,$check);

echo json_encode($data);

<强>的javascript

$.ajax ({
    type: "GET",
    url: "update_module.php",
    data: "name="+name+"&displayorder="+displayorder+"&id="+id+"&check="+check,

    success: function(msg)
        {
           msg = $.parseJSON(msg);
           alert("Display Order: "+msg.displayorder+"\n Module: "+msg.modulename);
        }       
});  

有关$.parseJSON

的更多信息

注意:使用此方法时,您必须确保仅返回/回显$data。如果还有其他内容(通知,警告,html等),JSON将会中断并且不会给你输出。