1

content.php

     $(document).ready(function(){
        var srt=$("#cal1Date1").val();//start date
        var end=$("#cal1Date2").val();//end date

                $.ajax({
            url:"http://localhost/show.php",
            data: {srt:srt,
                end:end
                    },
            type:"POST",
            dataType: "json",
            complete:function(response){

                console.log(response.responseText);
            }}); })}

the above code is sends the data to show.php which queries the date range

show.php

    include_once "connector.php";
    $sql = "SELECT * FROM `testtable` WHERE  `Request Date` BETWEEN 'start date' AND 'end date' "   ;

            $result = mysqli_query($db,$sql);

            if (mysqli_num_rows($result) > 0) {                         
              while($row = mysqli_fetch_assoc($result)) {
                 echo  $row["Order ID"]; }
                        mysqli_close($db);
                   ?>

and the above code filters the result by the date range we provided. it prints all the filtered 'Order Ids'

now I want to know is how can I send all these order id back to content.php? so i can manipulate the data there.

1
  • What you will echo in show.php will be available in ajax success callback. ... dataType: "json", success:function(r){ //+ r is the content echo from show.php } Commented Feb 5, 2015 at 7:55

1 Answer 1

2

You have to json_encode to return the response to ajax(content.php)

include_once "connector.php";
$startdate=$_POST['srt'];
$enddate=$_POST['end'];
$sql = "SELECT * FROM `testtable` WHERE  `Request Date` BETWEEN 'start   date'=$startdate AND 'end date'=$enddate "   ;

    $result = mysqli_query($db,$sql);

    if (mysqli_num_rows($result) > 0) {                         
      while($row = mysqli_fetch_assoc($result)) {
         $orderid[]= $row["Order ID"]; }
          mysqli_close($db);
}
     echo json_encode(array('OrderID'=>$orderid));  
           ?>

In ajax Complete function get the response as below,

complete:function(response){

      $rtndata=response.responseText;

          var dat1a=jQuery.parseJSON($rtndata); 

                   var result=dat1a.OrderID;
                   console.log(result[0]);
                   console.log(result[1]);
             console.log(result[2]);
    }
Sign up to request clarification or add additional context in comments.

6 Comments

their is some problem with json response because data is coming like this {"orderid":["95695","4428516","4428517"]} but when i put console.log(response.orderid); result is undefined
use var data=jQuery.parseJSON(response); console.log(data.orderid);
{"Order ID":["95695","4428516","4428517"]} this is the response I am Trying this ` console.log(data[1].);` so that it would print 95695 but it is just printing undefined
did u get data value as {"Order ID":["95695","4428516","4428517"]}.
Yes that's is what I am getting
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.