MySql data using JSON and AJAX in PHP
I want to get searched data from mysql database using JSON and show in my
php page. I was write this code but it not retrieve any data.please help
me
//Client Page
`$`(function ()
{
var roll = document.getElementById("roll").value;
`$`.ajax({
type:"POST",
url: 'api.php',
data: "roll=" +roll,
dataType: 'json',
success: function(data)
{
var id = data[0];
var vname = data[1];
`$`('#output').html("id: "+id+" name: "+vname);
}
});
}); `
//api.php
`$`host = "localhost";
`$`user = "root";
`$`pass = "";
`$`databaseName = "ajax";
`$`tableName = "stud";
`$`con = mysql_connect(`$`host,`$`user,`$`pass);
`$`dbs = mysql_select_db(`$`databaseName, `$`con);
if(isset($_POST['roll'])){
`$`data = `$`_POST['roll'];
`$`result = mysql_query("SELECT * FROM $tableName WHERE roll =
'".$data."'");
`$`array = mysql_fetch_row(`$`result);
}
echo json_encode(`$`array);`
No comments:
Post a Comment