dongmaoluan5719 2016-12-12 05:17 采纳率: 100%
浏览 49
已采纳

Ajax在php中发布未定义的索引

I have 2 php file. One Php file uses ajax to post something to another php file.

Let's say Php 1 is called index.php while the other php is getsomething.php

Here's my index.php code with ajax(only part of the code is given here)

$(function() {
  $("#datestart").on("change", function() {
     var d = new Date();
     var n = d.getFullYear();
     var dte = new Date(n, $(this).val(), 0).getDate();
     var datestart = n + "-" + $(this).val() + "-01 00:00:00";
     var dateend   = n + "-" + $(this).val() + "-" + dte + " 23:59:59";
     var brandid   = $('#txtbxbrandid').val();

     //-----GENDER----//
     $.ajax({ url: 'ajax/datagenderfilter.php',
         dataType:'json',
         type: 'post',
         data: {'bidg' : brandid, 'startg' : datestart, 'stopg' : dateend},

          success: function(output) {

          $('#chartContainergender').remove();
          $('#chartgender').append('<div id="chartContainergender" style="height: 300px; width: 100%;"></div>');
          var chartgender = new CanvasJS.Chart("chartContainergender",
            {
                title:{
                    text: "Pie chart filtered by Gender"
                },
                legend: {
                    maxWidth: 350,
                    itemWidth: 120
                },
                data: [
                {
                    type: "pie",
                    showInLegend: true,
                    legendText: "{indexLabel}",
                    dataPoints: output
                }
                ]
            });
            chartgender.render();
          },
          error: function(request, status, error){
            alert("Error: Could not delete");
          }
      });


   });
});

Now here's the getsomething.php

if (isset($_POST['bidg'])) {
$brandid = $_POST['bidg'];
$datebeg = $_POST['startg'];
$dateend = $_POST['stopg'];

echo json_encode($brandid, JSON_NUMERIC_CHECK);
}
else{
  echo json_encode("FAILED", JSON_NUMERIC_CHECK);
}

But everytime I tried posting it the getsomething.php skips the if and go straight to else.

Can someone check where am I going wrong.

  • 写回答

2条回答 默认 最新

  • duannaoye0732 2016-12-12 05:29
    关注

    I think some of the variables are undefined in your ajax call, try this:

    $brandid = isset($_POST['bidg']) ? $_POST['bidg']: '';
    $datebeg = isset($_POST['startg'])? $_POST['startg']: '';
    $dateend = isset($_POST['stopg'])? $_POST['stopg']: '';
    

    isset() is used to check if the variable is set or not. This will remove the error from your code.

    To check which variable is undefined, check your console or put a print_r($_POST), it will print all index available in POST

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

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多