PHP没有从$ .ajax获取POST数据

时间:2021-05-25 21:35:02

I have a JavaScript that runs a POST method once my datepicker has been out of focus (I also tried this on a regular submit button) and runs the script rent-fetch-pick-up-point.php. The PHP runs, however it doesn't get past the if-statement because my it's not getting the POST data. The datepicker is tied to a input field time-period-from

我有一个运行POST方法的JavaScript,一旦我的datepicker失焦(我也在常规提交按钮上尝试了这个)并运行脚本rent-fetch-pick-up-point.php。 PHP运行,但是它没有通过if语句,因为我没有得到POST数据。 datepicker绑定到输入字段time-period-from

datepickerTo.blur(function(){
  if (selectedDateFrom.length > 0) {

    datepickerFrom.delay(500).queue(function(){

      $.ajax({
        type: "POST",
        url: "include/rent-fetch-pick-up-point.php",
        data: {action: selectedDateFrom},
        success: function(data) {
          $("#pick-up-point-container").html(data);
        }
      });
    });
  }
});

Here is the PHP code:

这是PHP代码:

if (isset($_POST['time-period-from'])) {
  require '../include/connection.php';

  $dateFrom = $_POST['time-period-from'];
  $sql = "SELECT * FROM order WHERE $dateFrom BETWEEN date_from AND date_to";
  $result = mysqli_query($connection, $sql);
  $numRows = mysqli_num_rows($result);

  echo $sql; // For testing purposes
}

And here's the HTML:

这是HTML:

  <input type="text" name="time-period-from" id="datepicker-from" class="datepicker"></p>

I also tried using $.post() instead of $.ajax(), but I ran into the same issue:

我也尝试使用$ .post()而不是$ .ajax(),但我遇到了同样的问题:

$.post("include/rent-fetch-pick-up-point.php", {name: selectedDateTo}, function(data) {

  $("#pick-up-point-container").text(data)

});

3 个解决方案

#1


2  

The keys of $_POST come from the keys of the object you pass to the data: option, not the names of the form fields where the values originally came from. Since you used:

$ _POST的键来自传递给data:选项的对象的键,而不是最初来自的值的表单字段的名称。既然你用过:

data: { action: selectedDateFrom }

the value will be in $_POST['action'], not $_POST['time-period-from']. So you need to use:

该值将在$ _POST ['action']中,而不是$ _POST ['time-period-from']。所以你需要使用:

if (isset($_POST['action']))

and:

和:

$dateFrom = $_POST['action'];

or you could change the Javascript to:

或者您可以将Javascript更改为:

data: { "time-period-from": selectedDateFrom }

#2


0  

I think your selectedDateFrom variable is array that cause your post info can't you get properly .

我认为你的selectedDateFrom变量是一个数组,导致你的帖子信息无法正常。

data: {action: $('#selectedDateFrom').serializeArray()}

then you get your form data properly

然后你就可以正确获得表单数据了

#3


0  

You aren't grabbing the right variable on the PHP side:

您没有在PHP端抓取正确的变量:

if (isset($_POST['action'])) {
  require '../include/connection.php';

  $dateFrom = $_POST['action'];
  $sql = "SELECT * FROM order WHERE $dateFrom BETWEEN date_from AND date_to";
  $result = mysqli_query($connection, $sql);
  $numRows = mysqli_num_rows($result);
  echo $sql; // For testing purposes
}

#1


2  

The keys of $_POST come from the keys of the object you pass to the data: option, not the names of the form fields where the values originally came from. Since you used:

$ _POST的键来自传递给data:选项的对象的键,而不是最初来自的值的表单字段的名称。既然你用过:

data: { action: selectedDateFrom }

the value will be in $_POST['action'], not $_POST['time-period-from']. So you need to use:

该值将在$ _POST ['action']中,而不是$ _POST ['time-period-from']。所以你需要使用:

if (isset($_POST['action']))

and:

和:

$dateFrom = $_POST['action'];

or you could change the Javascript to:

或者您可以将Javascript更改为:

data: { "time-period-from": selectedDateFrom }

#2


0  

I think your selectedDateFrom variable is array that cause your post info can't you get properly .

我认为你的selectedDateFrom变量是一个数组,导致你的帖子信息无法正常。

data: {action: $('#selectedDateFrom').serializeArray()}

then you get your form data properly

然后你就可以正确获得表单数据了

#3


0  

You aren't grabbing the right variable on the PHP side:

您没有在PHP端抓取正确的变量:

if (isset($_POST['action'])) {
  require '../include/connection.php';

  $dateFrom = $_POST['action'];
  $sql = "SELECT * FROM order WHERE $dateFrom BETWEEN date_from AND date_to";
  $result = mysqli_query($connection, $sql);
  $numRows = mysqli_num_rows($result);
  echo $sql; // For testing purposes
}