douxian7117 2018-03-28 09:42
浏览 65
已采纳

php显示空数组但ajax显示数据

I am trying to use the data that AJAX sends in PHP but for some reason says that the array is empty I also don't get any errors from PHP or in my console. the console.log(data) shows the array with with the values of the selects. After the PHP there is HTML where the script.js is.console.logconsole.log

index.php

 <?php
    error_reporting(-1);
    $value1 = "";
    $value2 = "";
    print_r($_POST); 
    if (isset($_POST['date']) && isset($_POST['date'])) {
        if (isset($_POST['date'])) {
            echo "Yes, mail is set";
            $value1 = $_POST['date'];
            $value2 = $_POST['quantity'];
        } else {
            echo "No, mail is not set";
        }
        exit;
    }
    echo $value1; 
    echo $value2;
    $canmakereservation = "https://www.planyo.com/rest/?method=can_make_reservation&api_key=YOURKEY&resource_id=110556&start_time=2018-04-25 09:00&end_time=2018-04-25 12:00&quantity=5";
    $cleancanmakereservation = preg_replace("/ /", "%20", $canmakereservation);
    $reservationavailable = file_get_contents("$cleancanmakereservation");
    $reservationAvailable = json_decode($reservationavailable, true);
    // echo "$cleancanmakereservation";
    // var_dump($reservationAvailable);
 ?>

script.js

$(document).ready(function(){

var date = "date";
var begin = "begin";
var eind = "eind";
var aantal = "aantal";


$('#datum').change(function() {
  date = $("#datum").val();
  console.log(date);
});
$('#beginTijd').change(function(){
    begin =( $(this).val() ); 
    console.log(begin);      
});
$('#Tijdsduur').change(function(){
    eind =( $(this).val() ); 
    console.log(eind);      
});
$('#aantalSloepen').change(function() {
aantal = ($(this).val());
console.log(aantal);
  $.ajax({
      type: "POST",
      url: "index.php",
      data: {
          date: begin,
          quantity: aantal
      },
      success: function(data) {
          alert(data);
          console.log(data);
      }
  });
});
});
  • 写回答

2条回答 默认 最新

  • doupuzhimuhan9216 2018-03-28 09:50
    关注

    First! There are duplicate conditions isset($_POST['date']) (3 times) in your code:

     if (isset($_POST['date']) && isset($_POST['date'])) {
            if (isset($_POST['date'])) {
                echo "Yes, mail is set";
                $value1 = $_POST['date'];
                $value2 = $_POST['quantity'];
            } else {
                echo "No, mail is not set";
            }
            exit;
        }
    

    Second! You should remove exit; and try again!

    Third: You should check your variable type, if your $value1 and $value2 is not a string, numeric. echo is false. try with return

    EDIT:

    Try this, it worked for me! the .php destination:

    $value1 = "";
    $value2 = "";
    if (isset($_POST['date']) && isset($_POST['quantity'])) {
    
            echo "Yes, mail is set";
            $value1 = $_POST['date'];
            $value2 = $_POST['quantity'];
        }
        else {
            echo "No, mail is not set";
    }
    echo $value1;
    echo $value2;
    

    The ajax:

    enter image description here

    The result

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog