将对象数据从AJAX发送到PHP [英] Sending Object Data from AJAX to PHP

查看:53
本文介绍了将对象数据从AJAX发送到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send data to a php file to save in database, but I don't have any response. If a checkbox is check, the [obj][idCheckbox] = 1, else [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文件。

the savePermissions.php file.

        $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());

谢谢。

推荐答案

PHP很有意思,因为它涉及Ajax时不会像其他表单那样从 $ _ POST 中提取。你实际上需要从读取输入://输入

PHP is kind of interesting in that it doesn't pull from $_POST like other forms when Ajax is involved. You actually will need to read the input from 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命令来删除不良字符并清理输入。

You can add the filter_var command to strip bad characters and sanitize the input.

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

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

While debugging this I would highly recommend using Chrome's Developer mode with the 'network' tab. Find your ajax call near the bottom and you can view exact header info.

这篇关于将对象数据从AJAX发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆