Jeancarlo C. Jeancarlo C. - 5 months ago 20
jQuery Question

Success ajax request but can't see my data

So I try to call data from MySQL database using jQuery.ajax but I'm not able to see the result in my web. I have my estacionamiento table here. Then my api.php file and index.php respectively



<?php
include("connect.php");
$link=Connection();

$result = mysql_query("SELECT `estado` FROM `estacionamiento` ", $link); //query
$array = mysql_fetch_row($result); //fetch result

echo json_encode($array);

?>


<script src="js/jQuery.js"></script>
<script type="text/javascript">

$(function ()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
$.ajax({
url: 'api.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
alert('Successfully called');
console.log(data);
$('#output').html("<b> estado: </b>"+data);
}
});
});

</script>


I'm able too see the alert successfully called but in console log it shows that the data is [""] but it's clearly that I have some data in 'estado'
You can see the result here. (http://estacionamiento.site88.net/)

Edit: Now I have to fetch many variables, I would receive any suggestion

I was thinking something like this but It isn't working

<?php
include("connect.php");
$link=Connection();
$data= array();
$result = mysqli_query($link, "SELECT * FROM `estacionamiento' ORDER BY timeStamp DESC",
while ($array = mysqli_fetch_array($result)){
$data[] = $array;
}

echo json_encode($data);

?>

Answer

Your api.php, I guess, will only return ["libre"]. Or you want only a single data to be returned?

<?php

    /* ESTABLISH CONNECTION; REPLACE NECESSARY PARAMETERS BELOW */
    $link = new mysqli("Host", "Username", "Password", "Database");

    /* CHECK CONNECTION */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $stmt = $link->prepare("SELECT estado FROM estacionamiento WHERE estado <> '' ORDER BY estado DESC"); /* PREPARE YOUR QUERY */
    $stmt->execute(); /* EXECUTE QUERY */
    $stmt->bind_result($estado); /* BIND RESULT TO THIS VARIABLE */
    $stmt->fetch(); /* FETCH RESULT */
    $stmt->close(); /* CLOSE PREPARED STATEMENT */

    echo $estado;

?>

Then, on your ajax (since, you'll be returning a single data that is not an array, I removed the dataType: 'json'):

$.ajax({
    url: 'api.php',  
    success: function(data)
    {
        alert('Successfully called');
        console.log(data); /* WILL RETURN AS `libre` */
        $('#output').html("<b> estado: </b>"+data); /* #output WILL HAVE `estado: libre` */
    }
});

Maybe you'll be interested with jQuery.get since you're only getting a single data:

$.get( "api.php", function( data ) {
    alert("Successfully called");
    console.log(data);
    $("#output").html('<b> estado: </b>'+data);
});