2

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.

Erluan
  • 31
  • 6
  • remove the brackets around the object which sent from ajax; , `data:{}` not `data:({})` – hassan Mar 08 '17 at 17:08
  • It didn't work :S – Erluan Mar 08 '17 at 17:13
  • what's the output of `print_r($_POST);` ? and be careful, your code is so vulnerable to sql injection ! and mysql_* has been deprecated and your must stop using it – hassan Mar 08 '17 at 17:17
  • At the end of the savePermissions.php, Hassan? – Erluan Mar 08 '17 at 17:42
  • if you run `echo ini_get('enable_post_data_reading')` does it return `false` (or blank/0)? if so, you could try to set [enable_post_data_reading](http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading)=true in php.ini (unless you don't have permissions to do so) – Sᴀᴍ Onᴇᴌᴀ Mar 13 '17 at 21:26

1 Answers1

2

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.

John Pavek
  • 2,595
  • 3
  • 15
  • 33
  • 1
    It might actually be a form encoding issue (see [this answer](http://stackoverflow.com/a/8893792/1575353)) or if `enable_post_data_reading=false` in php.ini then $_POST will be empty (see http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading) - So your claim "_PHP is kind of interesting in that it doesn't pull from $_POST like other forms when Ajax is involved_" might not always be true – Sᴀᴍ Onᴇᴌᴀ Mar 13 '17 at 21:31