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


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();

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

          success: function(output) {

          $('#chartgender').append('<div id="chartContainergender" style="height: 300px; width: 100%;"></div>');
          var chartgender = new CanvasJS.Chart("chartContainergender",
                    text: "Pie chart filtered by Gender"
                legend: {
                    maxWidth: 350,
                    itemWidth: 120
                data: [
                    type: "pie",
                    showInLegend: true,
                    legendText: "{indexLabel}",
                    dataPoints: output
          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);
  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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dqjo4340 2016-12-12 05:36

    I would suggest taking your

    data: {
        'bidg' : brandid, 
        'startg' : datestart, 
        'stopg' : dateend

    object of your $.ajax call, and make it a variable before the call.


    var _data = {'bidg' : brandid, 'startg' : datestart, 'stopg' : dateend};
    console.log("data for ajax call", _data);

    then your ajax call becomes

    $.ajax({ url: 'ajax/datagenderfilter.php',
             type: 'post',
             data: _data,
    // the rest of your code here

    This way you can open the web developer console, and select 'persist logs' and you can see your console log even after your page goes somewhere else. This way you can see if all of your variables are set the way you expect them to be.

    I would have to agree with the others, that it would seem one or more of your vars are undefined when it gets to the if statement in your PHP. This way you can examine what your data looks like before the ajax call.




  • ¥15 stata合并季度数据和日度数据
  • ¥15 c语言练习:统计词频
  • ¥15 谁能提供rabbitmq,erlang,socat压缩包,记住版本要对应,发到邮箱
  • ¥15 谁能提供rabbitmq,erlang,socat压缩包,记住版本要对应
  • ¥15 Vue3 中使用 `vue-router` 只能跳转到主页面?
  • ¥15 用QT,进行QGIS二次开发,如何在添加栅格图层时,将黑白的矢量图渲染成彩色
  • ¥50 监控摄像头 乐橙和家亲版 保存sd卡的文件怎么打开?视频怎么播放?
  • ¥15 Python的Py-QT扩展库开发GUI
  • ¥60 提问一下周期性信信号的问题
  • ¥15 jtag连接不上fpga怎么办