Reputation: 830
I don't know what's wrong, the question is i'm trying to return the values from a mysql query for print in a html div (#receptor) but is not printing any value, i'm trying to make a call after make the request to an external function define below for print the JSON source, this is what i'm trying to do for now:
result.php
<?php
$library = "DbConnection.inc.php";
if (is_readable($library)){
require $library;}else{
throw new RuntimeException("No se Pudo Incluir la ${library}");
}
$db = new DbConnection('localhost','root','','macrotelecom');
switch (@$_REQUEST['action'])
{
case "consultar" :
$db->connect();
$data = $db->getAllRows("SELECT * FROM Caracter");
$db->disconnect();
echo json_encode($data);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src='jquery-1.7.1.min.js' ></script>
<script type="text/javascript">
$(function(){
$("#consultar").click(function(){
$("#receptor").text("Consultando....");
$.post("result.php",{action:"consultar"}, respuesta,'json')
});
});
function respuesta(arg){
$("#receptor").html(arg.toSource());
}
</script>
</head>
<body>
</br>
<input type="button" id="consultar" value="consultar">
<div id="receptor" style="border: solid 1px black; height: 100%; width: 100%;">
</div>
</body>
</html>
But, if i make a call within $.post function, it works, but i don't want to do this:
<?php
$library = "DbConnection.inc.php";
if (is_readable($library)){
require $library;}else{
throw new RuntimeException("No se Pudo Incluir la ${library}");
}
$db = new DbConnection('localhost','root','','macrotelecom');
switch (@$_REQUEST['action'])
{
case "consultar" :
$db->connect();
$data = $db->getAllRows("SELECT * FROM Caracter");
$db->disconnect();
echo json_encode($data);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src='jquery-1.7.1.min.js' ></script>
<script type="text/javascript">
$(function(){
$("#consultar").click(function(){
$("#receptor").text("Consultando....");
$.post("result.php",{action:"consultar"}, respuesta,'json')
});
});
function respuesta(arg){
$("#receptor").html(arg.toSource());
}
</script>
</head>
<body>
</br>
<input type="button" id="consultar" value="consultar">
<div id="receptor" style="border: solid 1px black; height: 100%; width: 100%;">
</div>
</body>
</html>
What's wrong, this drive my nuts seriously, i can't undestand why is not print values, i define de datatype as json and nothing happens, i passed the paramater in the call "respuesta" like respuesta(data) and neither, i hope someone can give me a hand, i'm not able to see the solution.
I put the library code for database connection that i'm using for if you want to make local tests.
DbConnection.inc.php
<?php
class DbConnection {
private $db_connection = null;
private $db_host = '';
private $db_user = '';
private $db_password = '';
private $db_name = '';
private $errors = array();
public function __construct($db_host, $db_user, $db_password, $db_name)
{
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_password = $db_password;
$this->db_name = $db_name;
}
public function connect()
{
if ( !$this->db_connection = @mysql_connect($this->db_host, $this->db_user, $this->db_password) ) {
throw new RunTimeException("Couldn't connect to the database server");
}
if ( !@mysql_select_db($this->db_name, $this->db_connection) ) {
throw new RunTimeException("Couldn't connect to the given database");
}
$this->executeQuery("SET CHARACTER SET 'utf8'");
}
public function disconnect(){
if(mysql_close($this->db_connection)){
return true;
}
return false;
}
public function getAllRows($sql)
{
if ( !$results = @mysql_query($sql, $this->db_connection) ) {
throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
}
$count = 0;
$rows = array();
while ( $row = mysql_fetch_assoc($results) ) {
$rows[] = $row;
$count++;
}
return ($count)?$rows:false;
}
public function getOneColumn($sql)
{
if ( !$results = @mysql_query($sql, $this->db_connection) ) {
throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
}
$count = 0;
$rows = array();
while ( $row = mysql_fetch_array($results) ) {
$rows[] = $row[0];
$count++;
}
return ($count)?$rows:false;
}
public function getArrayPair($sql)
{
if ( !$results = @mysql_query($sql, $this->db_connection) ) {
throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
}
$count = 0;
$rows = array();
while ( $row = mysql_fetch_array($results) ) {
$rows[$row[0]] = $row[1];
$count++;
}
return ($count)?$rows:false;
}
public function getOneRow($sql)
{
if ( !$results = @mysql_query($sql, $this->db_connection) ) {
throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
}
if ( $row = mysql_fetch_assoc($results) ) {
return $row;
}
return false;
}
public function getOneValue($sql)
{
if ( !$results = @mysql_query($sql, $this->db_connection) ) {
throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
}
if ( $row = mysql_fetch_array($results) ) {
return $row[0];
}
return false;
}
public function executeQuery($sql)
{
if ( !@mysql_query($sql, $this->db_connection) ) {
$this->errors[] = mysql_error($this->db_connection);
return false;
}
return true;
}
public function getErrors()
{
return $this->errors;
}
public function getLastId()
{
return mysql_insert_id($this->db_connection);
}
public function countRows($table)
{
if (!is_string($table)) {
throw new InvalidArgumentException("table_name isn't an string");
}
if ( !$results = @mysql_query("SELECT COUNT(*) as total FROM $table", $this->db_connection) ) {
throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
}
$count = mysql_fetch_array($results);
$count = $count['total'];
return ($count)?$count:0;
}
}
?>
Upvotes: 1
Views: 383
Reputation: 1343
You can also use it with this
var id=$("#id").attr("value");
$.getJSON("pages.php",{id:id},dates);
function dates(datos)
{
$("#list").html("Name:"+datos[1].name+"<br>"+"Last Name:"+datos[1].lastname+"
<br>"+"Address:"+datos[1].address);
}
more help you will find it on here
http://docs.jquery.com/Getjson
hope this will help
Upvotes: 1