Home:ALL Converter>Post to php file, and retrieve data with javascript

Post to php file, and retrieve data with javascript

Ask Time:2017-05-05T03:28:52         Author:Emerenciana

Json Formatter

So i have a php file, that prints data from a table encoded on JSON format.

This is the php file:

<?php
include "db.php";
 $id=$_POST['id'];
$data=array();
$q=mysqli_query($con,"select * from `sitios` where `id_sitio`='$id'");

while ($row=mysqli_fetch_object($q)){
 $data[]=$row;
}

if($q)
 echo "success";
 else
 echo "error";
 }

echo json_encode($data);
?>

This is the javascript script:

$(document).ready(function() {
        var id = decodeURI(getUrlVars()["id"]);
         var dataString = "id=" + id;
        $.ajax({
                type: "POST",
                url: "http://pedrofidalgo.pt/bilapoint/listar_sitio_single.php",
                data: dataString,
                crossDomain: true,
                cache: false,
                success: function(data) {
                    if (data == "success") {
                                    $.getJSON(url, function(result) {
                                        console.log(result);
                                        $.each(result, function(i, field) {
                                            var id = field.id_sitio;
                                            var nome = field.nome;
                                            var descricao = field.descricao;
                                            var img = field.img;
                                            var morada = field.morada;
                                            var coordenada_x = field.coordenada_x;
                                            var coordenada_y = field.coordenada_y;
                                            document.getElementById("titulo").innerHTML =  nome;
                                            document.getElementById("desc").innerHTML =  descricao;
                                            document.getElementById("morada").innerHTML =  morada;
                                                }); 
                                            });
                    } else if (data == "error") {
                        alert("error");
                    }
                }
            });

    });

So basically i a have where i have all items from the database select (list_all.php), and then when i click on a single item, the ID of that item is passed on the URL, and i retrieve it on the otherside with javascript. I dont use GET because this is with phonegapp, so i use a .js file called getURI.js.

First, the function gets the ID that was passed. Then it posts to the PHP file, and the PHP file will get the ID, and make the query for that single item on the database. Is successed, i wanted to store all that data on variables. But for some reason, im getting an error on the console saying

POST http://192.168.1.241:3000/proxy/http%3A%2F%2Fpedrofidalgo.pt%2Fbilapoint%2Flistar_sitio_single.php 500 (Internal Server Error)

THe server is responding correctly because others scripts on the app are working.

Author:Emerenciana,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43791303/post-to-php-file-and-retrieve-data-with-javascript
yy