将对象数据从AJAX发送到PHP

时间:2017-03-08 17:01:53

标签: javascript php jquery json ajax

我试图将数据发送到php文件以保存在数据库中,但我没有任何回复。如果选中复选框,则[obj] [idCheckbox] = 1,否则[obj] [idCheckbox] = 0。

发送

的文件
    var i=0;
    var objetoTodasPermissoes = function(){};

    var objTodasPermissoes = new objetoTodasPermissoes();

    $.each($(".classePermissoes"), function(){
      objTodasPermissoes[$(this)[0].id] = 0
      i++;
    });


    $.each($(".classePermissoes:checked"), function(){
        alert('ok');
        objTodasPermissoes[$(this)[0].id] = 1;
    });

    console.log(objTodasPermissoes);

    $.each($("#userList tr"),function(){
        alert(this.id);
        var iduser = this.id;
    $.ajax({
            url:'../json/usuarioperm/savePermissions.php',
            data:({
            idusuario:iduser,
            objTodasPermissoes:objTodasPermissoes,
            }),
            success:function(a){
            Alert("Saved!");
            }
    });
    });
}

savePermissions.php文件。

        $iduser = $_POST["iduser"];

        $perm_usuarios = $_POST["objTodasPermissoes"]["perm_usuarios"];
        $perm_importar = $_POST["objTodasPermissoes"]["perm_importar"];
        $perm_log = $_POST["objTodasPermissoes"]["perm_log"];
        $perm_proto = $_POST["objTodasPermissoes"]["perm_proto"];
        $perm_limpeza = $_POST["objTodasPermissoes"]["perm_limpeza"];
        $perm_lixeira = $_POST["objTodasPermissoes"]["perm_lixeira"];
        $perm_relatusuarios = $_POST["objTodasPermissoes"]["perm_relatusuarios"];
        $perm_deptos = $_POST["objTodasPermissoes"]["perm_deptos"];
        $perm_deptospastas = $_POST["objTodasPermissoes"]["perm_deptospastas"];
        $perm_empresas = $_POST["objTodasPermissoes"]["perm_empresas"];



        mysql_query("UPDATE hospital.users set 
        perm_usuarios=".$perm_usuarios.",
        perm_importar=".$perm_importar.",
        perm_log=".$perm_log.",
        perm_proto=".$perm_proto.",
        perm_limpeza=".$perm_limpeza.",
        perm_lixeira=".$perm_lixeira.",
        perm_relatusuarios=".$perm_relatusuarios.",
        perm_deptos=".$perm_deptos.",
        perm_deptospastas=".$perm_deptospastas.",
        perm_empresas=".$perm_empresas." where id=".$iduser) or die (mysql_error());

谢谢。

1 个答案:

答案 0 :(得分:2)

PHP非常有趣,因为当涉及Ajax时,它不像其他形式那样从$_POST拉出来。实际上你需要阅读php://input

的输入

这是一个很小的例子

$data = file_get_contents("php://input");
$response = json_decode($data, true ); // True converts to array; blank converts to object

$emailAddr = $response["email"];

希望你能成功申请。

编辑:您可以添加filter_var命令来删除不良字符并清理输入。

$emailAddr = filter_var($response["email"], FILTER_SANITIZE_EMAIL);
$firstName = filter_var($response["firstName"], FILTER_SANITIZE_STRING);

在调试时,我强烈建议您使用Chrome的开发者模式,使用'网络'标签。在底部附近找到你的ajax电话,你可以查看确切的标题信息。