简体   繁体   中英

Ajax post undefined index in 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.

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

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.

So...

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',
         dataType:'json',
         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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM