I'm trying to make a application using phonegap. I am trying to make a select using php return the result to ajax function and print the result...but I don't know if I am doing right and what I need to put in my html code to show the result.
AJAX CODE:
$(document).ready(function() {
$.ajax({
url : 'http://ip/again/www/index.php',
dataType : "json",
success : function(data){
var html = "";
for($i=0; $i < data.length; $i++){
html += "<strong>Nome:</strong> "+data[$i].nome +" "+
data[$i].sobreNome;
html += " <strong>Cidade:</strong> "+data[$i].cidade;
html += "<br />";
}
$('body').html(html);
}
});
});
</script>
PHP CODE:
<?php
$hostname = 'localhost';
$username = 'root';
$password = 'pass';
$database = 'mydb';
try {
$pdo = new PDO("mysql:host=$hostname;dbname=$database", $username,
$password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
//echo 'Conexao efetuada com sucesso!';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$sql = 'SELECT * FROM incidente ORDER BY codigo' ;
try {
$recebeConexao = $pdo->prepare($sql);
$recebeConexao->execute();
$result = $recebeConexao->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
$incid=$row;
echo (json_encode($incid));
}
} else {
$incid=0;
}
} catch (PDOException $ex) {
echo "Erro no Banco";
}
?>