Sending Object Data from AJAX to PHP

前端 未结 1 469
面向向阳花
面向向阳花 2021-01-23 00:04

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.

<
1条回答
  •  伪装坚强ぢ
    2021-01-23 01:04

    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.

    0 讨论(0)
提交回复
热议问题