0

I have a form in an html file which sends data to a php file. That php file selects from the database, transforms the results into a json file and sends it back to the html file.

I would like to know how can I get the json from the php; I do get the reply (from the network tab in the inspection menu), but I don't know how to get it (to echo/alert/make a chart with it). Thanks in advance!

This is my code to send the data:

<script type="text/javascript">
    $( function() {
        $( ".datepicker" ).datepicker({
            minDate: new Date(2015, 8 - 1, 25),
            maxDate: "0"
        });
    });

$(function () {
    // On form's submit, takes both input's values...
    $('form').on('submit', function (e) {
        var from = $("#from").val();
        var to = $("#to").val();
        // ...to compare them and displays an error if "to" is smaller than "from"
        if(Date.parse(from) > Date.parse(to)){
            alert("Invalid Date Range");
        }
        else{
            e.preventDefault();
            $.ajax({
                type: 'post',
                url: 'classes/Select.php',
                data: $('form').serialize(),
                success: function () {
                    alert('data sent');
                }
            });
        }
    });
});
</script>

And this is the php part that makes the json (left out all the mysql part and such):

while ($arr = $query->fetch(PDO::FETCH_ASSOC)) {
    echo json_encode($arr);
4
  • stackoverflow.com/help/mcve Commented Apr 18, 2017 at 8:17
  • Have you tried using json_encode()? Can you write your code here? Commented Apr 18, 2017 at 8:18
  • You need to show us example code of your situation. Commented Apr 18, 2017 at 8:19
  • Added some code, sorry for not including it! Commented Apr 18, 2017 at 8:23

3 Answers 3

1

Since you are using a loop in your php, create an array that will hold the results, when the loop finishes echo the results with json_encode;

$results = [];
while ($arr = $query->fetch(PDO::FETCH_ASSOC)) {
    $results[] = $arr;
}
echo json_encode($results);

Then in your javascript

    $.ajax({
       type: 'post',
       url: 'classes/Select.php',
       data: $('form').serialize(),
       success: function(data) {
           var results = JSON.parse(data); // convert the results to json
           console.log(results);
       }
   });
Sign up to request clarification or add additional context in comments.

2 Comments

It works! Might be a dumb question, but why do I have to change my php code? Only to make objects out of the json file?
@Newwt so the data can be compatible with both javascript and php. don't forget to mark it as an answer.
1

In your javascript ajax call code, you probably have a success: key. Try:

success: function(data) { console.log(data); }

2 Comments

Nope, doesn't work. It does log the data I send, not the data I receive.
Your code in the post has the success: function() with no 'data' in the brackets
1

if you get a JSON string use json_decode($json) and you get the value you want:

$json = '{"key-example": 12345}';

$obj = json_decode($json);
print $obj->{'key-example'}; // 12345

2 Comments

I already do, the thing is in the html side, where I don't know what to do to get the php-generated json.
make an ajax call and print the response data after convert it into a json with: JSON.parse(data)

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.