How can I use json response from php in jquery?

111
March 19, 2022, at 01:30 AM

I'm trying to do a website. I need to get all the reviews in the database. My initial guess was to use php like this:

function get_all_reviews(){
    $db = dbConnection();
    $db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $query = "SELECT Nome, Title, reviews.Review, users.Email FROM `users` JOIN `reviews` WHERE users.Email = reviews.Email; ";
    $statement = $db -> prepare($query);
    $statement->execute();
    $result = $statement->fetchAll(); 
    foreach ($result as $row) {
        $name = $row["Nome"];
        $title = $row["Title"];
        $review = $row["Review"];
        $email = $row["Email"];
        ?>
    <div class="box" id="<?= $email ?>">
    <div class="nome"><?= $name ?>  dice:</div>
    <div class="titolo"><?= $title ?> </div>
    <div class="recensione"><?= $review ?> </div>
    <button class="admin_bottom">X</button>
  </div>
    <?php    
}

But since it's a project for the university, I need to not use it (it's mandatory). I need to send the request with jquery, then with php I should send the response with json, but I don't get how to recall the reviews and list them. I try with this with php:

function gett_all_reviews(){
        $db = dbConnection();
        $db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $query = "SELECT Nome, Title, reviews.Review, users.Email FROM `users` JOIN `reviews` WHERE users.Email = reviews.Email; ";
        $statement = $db -> prepare($query);
        $statement->execute();
        $result = $statement->fetchAll(); 
        $resultSet = '{ ';
        foreach ($result as $row) {
            $resultSet .= '{"name": "'.$row["Nome"].'",';
            $resultSet .= '"title": "'.$row["Title"].'",';
            $resultSet .= '"review": "'.$row["Review"].'",';
            $resultSet .= '"email": "'.$row["Email"].'"},';
    
        }
        $resultSet .= '}';
        $db = null;
    
        
        return $resultSet;
    }
    $s = get_all_reviews();
    
    echo $s;

and with jquery:

$.getJSON( "../php/get_reviews.php", function( json ) {
        
        for (let index = 0; index < json.length; index++) {
            $("#conteiner_reviews").append(
                "<div class=\"box\" id= \""+ json[index].email+ "\">"+
                    "<div class=\"nome\">"+json[index].name +" dice:</div>" +
                    "<div class=\"titolo\"><"+json[index].title+"</div>"+
                    "<div class=\"recensione\">"+json[index].review +"</div></div>");
        }
    });

But doesn't work.

Answer 1
function gett_all_reviews(){
    $db = dbConnection();
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $query = "SELECT Nome as name, Title as title, reviews.Review as review, users.Email as email FROM `users` JOIN `reviews` WHERE users.Email = reviews.Email; ";
    $statement = $db -> prepare($query);
    $statement->execute();
    $result = $statement->fetchAll(); 
    $db = null;
    return $result;
}
echo json_encode(get_all_reviews());
$.getJSON('../php/get_reviews.php', function (data) {
  $.each(data, function (item) {
    $('#conteiner_reviews').append(
      '<div class="box" id="' + item.email+ '">' +
      '<div class="nome">' + item.name + 'dice:</div>' +
      '<div class="titolo">' + item.title + '</div>' +
      '<div class="recensione">' + item.review + '</div>'
    );
  });
});
Rent Charter Buses Company
READ ALSO
How can i do onclick event with html,css,js

How can i do onclick event with html,css,js

I want the css codes of the blog1popup class to change when the image is clickedI know how to do this with hover, but i don't know how to make this action happen by clicking

111
Best practices writing JavaScript that uses AJAX call to decide if default action should be prevented

Best practices writing JavaScript that uses AJAX call to decide if default action should be prevented

I'm using JavaScript and jQuery to write a handler for my form being submittedBased on certain conditions, the handler may allow or prevent the form from submitting

91
Woocommerce cart page custom js not working after applying coupon. I send cart data to external domain on proceed to checkout

Woocommerce cart page custom js not working after applying coupon. I send cart data to external domain on proceed to checkout

I am sending the cart data of woocommerce to an externally hosted checkout page with the click of the 'proceed to checkout buttonI just create inputs by looping the table and changing the cart form action URL to my external checkout page URL

144
Firefox treating pasted image from clipboard as a string instead of file

Firefox treating pasted image from clipboard as a string instead of file

I am trying to get image from on paste event from a contenteditable divIt is working fine in chrome but isn't working in firefox

130