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.

5
  • remove the brackets around the object which sent from ajax; , data:{} not data:({}) Commented Mar 8, 2017 at 17:08
  • It didn't work :S Commented Mar 8, 2017 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 Commented Mar 8, 2017 at 17:17
  • At the end of the savePermissions.php, Hassan? Commented Mar 8, 2017 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=true in php.ini (unless you don't have permissions to do so) Commented Mar 13, 2017 at 21:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It might actually be a form encoding issue (see this answer) or if enable_post_data_reading=false in php.ini then $_POST will be empty (see 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.