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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行