Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

javascript - Sending Object Data from AJAX to PHP

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.

File that sends

    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!");
            }
    });
    });
}

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

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Here is a tiny example

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

$emailAddr = $response["email"];

Hopefully you can apply that successfully.


Edit: 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);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...