Ajax Request

Sir I have these codes

[php]
function myCall() {

                var date_filter = $("#datepicker-13").val();

                alert(date_filter)
                var request = $.ajax({

                    url: "ajax_arrival.php",
                    type: "Get",
                    date: date_filter,
                    dataType: "html"
                });


                request.done(function(msg) {
                    $("#wh_bg1").html(msg);
                    alert(msg);
                });

                request.fail(function(jqXHR, textStatus) {
                    alert("Request failed: " + textStatus);
                });
            }
         
    </script>  [/php]

In above codes
alert(date_filter)
says: 29-08-2016

It means this funciton is getting DATE from FORM.

and here ajax_arrival.php for processing

[php]<?php
include_once(“includes/connectw.php”);

$query =“SELECT * FROM arrival WHERE date=‘date_filter’”;
$result=mysqli_query($con, $query);
if ($result){
$row = mysqli_fetch_array($result);
$res=$row[‘qty’]."##".$row[‘weight’];
echo $res;
}
?>[/php]

when I run function myCall()
then it says:

Udefined constant date_filter

why the value of date_filter is not going to ajax_arrival.php?
or is there anyother mistake?

Please help

First, I would use console.log(date_filter) instead of alert(date_filter), for it makes it easier to debug. You don’t get that annoying popup window and it can be easier seen in the javascript console that most browsers have.

[php] function readQuestion() {
var date_filter = $("#datepicker-13").val();
var params = {date_filter: date_filter}; // Set the date filter as an object in jQuery (JavaScript):
console.log(params); // See what params looks like in console in browser:
var myData = jQuery.param(params); // Set parameters to correct AJAX format;
$.ajax({
type: ‘post’, // I prefer POST over GET in php
url: ‘ajax_arrival.php’,
data: myData,
success: function (info) {
console.log(info);
$(’.result’).text("This is the result " + info.result);
},
error: function (request, status, error) {
if (request.responseJSON.type === ‘fail’) {
$(’.errorMsg’).text(‘You have an error somewhere!’);
}
}
}); // End of Ajax Function:
}[/php]

[php] <?php
include_once(“includes/connectw.php”);
/* Makes is so we don’t have to decode the json coming from JQuery */
header(‘Content-type: application/json’);

$date_filter = filter_input(INPUT_POST, ‘date_filter’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

/* I’ll let you figure out how to pull the results from the database table using $date_filter that you pulled in using JSON/Ajax */
$row = mysqli_fetch_array($result);
$res=$row[‘qty’]."##".$row[‘weight’];
$output = json_encode([‘result’ => $res]);
output($output);

/*

  • Set error code then send message to Ajax/JQuery
    */
    function errorOutput($output, $code = 500) {
    http_response_code($code);
    echo $output;
    }

/*

  • If everything validates OK then send success message to Ajax/jQuery
  • Thanks to JimL on this forum for providing/helping with this to me
    */
    function output($output) {
    http_response_code(200);
    echo $output;
    }
    }[/php]

Obviously this won’t work for some of it psuedo code and I am a little rusty on code syntax so there might be errors there as well. That’s why it’s nice to have a good IDE. ;D An like I said in one of my comments that JimL is another great user to obtain help.
HTH John

Sponsor our Newsletter | Privacy Policy | Terms of Service