0

This is my PHP Code contains an array of tokens

$date=$_POST['date'];

$query=mysqli_query($conn,"SELECT tokenno from at_booking where date='$date' and status='Booked'");
while ($row = mysqli_fetch_array($query)) {

    echo json_encode($row);

}

This is my ajax respone code

date_input.change(function () {
        $("#tokens").show();
        var data=$("#date").val();
        //alert("data"+data);
        $.ajax({
            type : 'POST',
            url : 'tokens.php',
            data : {date: data},

            success:function(response){
                alert("response= "+response);
            }
        });

    });

Iam getting response like this

response= {"0":"Token1","tokenno":"Token1"}{"0":"Token2","tokenno":"Token2"}{"0":"Token3","tokenno":"Token3"}{"0":"Token4","tokenno":"Token4"}{"0":"Token5","tokenno":"Token5"}{"0":"Token6","tokenno":"Token6"}{"0":"Token8","tokenno":"Token8"}{"0":"Token7","tokenno":"Token7"}{"0":"Token9","tokenno":"Token9"}{"0":"Token10","tokenno":"Token10"}

I bit confused how to split the response and i have to get output "Token1" from {"0":"Token1","tokenno":"Token1"} and "Token2" from {"0":"Token2","tokenno":"Token2"}

karthik
  • 3
  • 3
  • 10

2 Answers2

0

I think you want to do something more like this, and then go over the data with jquery .each()

$date=$_POST['date'];

$query=mysqli_query($conn,"SELECT tokenno from at_booking where date='$date' and status='Booked'");
while ($row = mysqli_fetch_array($query)) {

$rows[]=$row;

}
header('Content-Type: application/json');
echo json_encode($rows);
Funk Doc
  • 1,473
  • 1
  • 11
  • 12
0

You can push in array and then return it as json

$date=$_POST['date'];

$query=mysqli_query($conn,"SELECT tokenno from at_booking where date='$date' and status='Booked'");
while ($row = mysqli_fetch_array($query)) {

    $arr[] = $row;

}

    echo json_encode($arr);

in JS code try this

 var values = [];
date_input.change(function () {
        $("#tokens").show();
        var data=$("#date").val();
        //alert("data"+data);
        $.ajax({
            type : 'POST',
            url : 'tokens.php',
            data : {date: data},

            success:function(response){
                $.each(data,function(key,value){
                    values.push(value.tokenno);
                });

                console.log(values);


            }
        });

    });
  • iam getting response like this response= [{"0":"Token1","tokenno":"Token1"},{"0":"Token2","tokenno":"Token2"},{"0":"Token3","tokenno":"Token3"},{"0":"Token4","tokenno":"Token4"},{"0":"Token5","tokenno":"Token5"},{"0":"Token6","tokenno":"Token6"},{"0":"Token8","tokenno":"Token8"},{"0":"Token7","tokenno":"Token7"},{"0":"Token9","tokenno":"Token9"},{"0":"Token10","tokenno":"Token10"}].how to split the response and i have to get only token1 token2 – karthik Aug 01 '17 at 16:39
  • yes you got an array of object is you use $.each you can split the result see my edit – Abderrahim Soubai-Elidrisi Aug 01 '17 at 16:40
  • could you tell me pls how to split the result – karthik Aug 01 '17 at 16:58
  • split is converting string to Array and response here is an array please use my javascript code and if you want an advanced exemple i will give it to you – Abderrahim Soubai-Elidrisi Aug 01 '17 at 17:19
  • Please give that example dude – karthik Aug 01 '17 at 17:30
  • check this https://jsfiddle.net/1tgLqkhn/ and i EDITED My Answer . if help please set my anwser as accepted anwser – Abderrahim Soubai-Elidrisi Aug 02 '17 at 09:31