-1

I'm to trying make a login using AJAX. AJAX function post data to PHP file but when I try to retrieve data from POST in PHP it's empty.

This is my AJAX posting code:

function validateUser() {
  var userR = $("#fldUser").val();
  var passwordR = $("#fldPassword").val();
  if (userR.length < 1) {
    $("#divStatusText").html("<p class=\"text-danger\"> <b>A user is required.</b></p>");
  } else if (passwordR.length < 1) {
    $("#divStatusText").html("<p class=\"text-danger\"><b>A password is required.</b></p>");
  } else {
    // alert(userR + passwordR)
    $.ajax({
      type: 'POST',
      // url: '/_Controlador/_GSystemLogIn.php/',
      url: '/_Controlador/_GSystemLogIn.php',
      // data:'user=' + userR +'&password=' + passwordR,
      data: {
        'user': userR,
        'password': passwordR
      },
      dataType: 'JSON',
      beforeSend: function(xhr) {
        alert("En before send: " + userR + passwordR);
      },
      success: function(data) {
        alert(data);
        if (data.message === "Success") {
          // location.href = "main.php";
        } else {
          $("#divStatusText").html("<p class=\"text-danger\"><b>User or password is incorrect.</b></p>");
          $("#fldUser").val("");
          $("#fldPassword").val("");
        }
      },
      error: function(data) {
        alert("Error in AJAX call.");
      }
    });
  }
}

PHP code retrieving data:

var_dump($_POST);

$user = $_POST['user'];
$password = $_POST['password'];

echo( "PHP user received: ". $user);
echo( "PHP pass received: ".$password);

Alert in AJAX beforeSend prints data correctly but var_dump($_POST) prints:

array(0) { }

I've also tried different ways to send data and URL. I'll really appreciate your help. Thank you!

freginold
  • 3,946
  • 3
  • 13
  • 28
  • Try without the quotes `data:{user : userR, password : passwordR},` and verify that you are accessing the right url – Rotimi Jan 24 '18 at 18:58
  • PHP can't access JSON posts using $_POST. See https://stackoverflow.com/questions/18866571/receive-json-post-with-php (which I believe this question is essentially a duplicate of) – Dave Jan 24 '18 at 19:23
  • 1
    It worked! I deleted dataType: 'JSON' from AJAX send and now I receive the data in php code correctly. Thank you so much! – Jonathan Camacho Jan 24 '18 at 19:31
  • 1
    @JonathanCamacho then your problem was likely cache or misinterpretation of results, the `dataType` option has nothing to do with php receiving your data. – Kevin B Jan 24 '18 at 19:41
  • I'm debbuging code with chrome developer tools this is happening: Chrome response returns in console the correct user and password that I received with array(2) {} in PHP code but, webpage prints "Notice: Undefined index: user in.." and "Notice: Undefined index: password in...".also var dump is still array(0) { } . I really don't know if the information is being received or not.. – Jonathan Camacho Jan 24 '18 at 21:00

2 Answers2

-1

You have to decode the data first. Just do -

$_POST = json_decode(file_get_contents('php://input'), true);

Here's the full explanation - https://stackoverflow.com/a/39508364/5400741

-1

In your ajax request,

type: 'POST' has to be method: 'POST'

Look at here

Mojtaba
  • 4,852
  • 5
  • 21
  • 38