4 Replies - 353 Views - Last Post: 13 March 2018 - 06:23 AM

#1 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

How to get the SQL query results back to AJAX?

Posted 12 March 2018 - 09:45 AM

I was looking for a way to periodically change website content (div tag in this example), and found out this code template to do it:

        <script>
            var set_delay = 5000,
                    callout = function () {
                        $.ajax({
                            url: "get_data.php",
                        })
                                .done(function (response) {
                                    $("#change_this_div").html(result);
                                })
                                .always(function () {
                                    setTimeout(callout, set_delay);
                                });
                    };

// initial call
            callout();
        </script>


Now, the get_data PHP file looks like that:

        <?php
        require_once 'connect.php';

        $sql = "SELECT firstname FROM users WHERE id = :id";
        $stmt = $conn->prepare($sql);
        $param_id = //some_id here
        $stmt->bindParam(':id', $param_id, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        ?>


How do I return the $result, which is the user's first name and display it in the div that I want to change? I can't find out

thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How to get the SQL query results back to AJAX?

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2334
  • View blog
  • Posts: 7,113
  • Joined: 15-January 14

Re: How to get the SQL query results back to AJAX?

Posted 12 March 2018 - 10:52 AM

Put all of the data you want to return into an array or object, you can give the keys meaningful names. Then you can use json_encode to turn the array or object into a string that you can use in Javascript to rebuild the same array/object structure. JSON is used all the time for transferring data like that.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4230
  • View blog
  • Posts: 13,411
  • Joined: 08-June 10

Re: How to get the SQL query results back to AJAX?

Posted 13 March 2018 - 01:03 AM

Mind that you should not use .html() directly on JSON.
Was This Post Helpful? 1
  • +
  • -

#4 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Re: How to get the SQL query results back to AJAX?

Posted 13 March 2018 - 06:04 AM

What should I do then before doing the .html()?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4230
  • View blog
  • Posts: 13,411
  • Joined: 08-June 10

Re: How to get the SQL query results back to AJAX?

Posted 13 March 2018 - 06:23 AM

process the JSON data and use the necessary methods you need to do so.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1