0
function start(){

$('#detailsPage').live('pageshow', function(event) {
    var user_name = getUrlVars()["user_name"];
    //var user_name = "studentB";
    $.getJSON('http://mydomain.com/getStudent.php?user_name='+user_name+'&jsoncallback=?', displayStudent);
});
}

above is the js and below is the php

<?php 

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Content-type: application/json");

include('mysqlConfig.php');

$user_name = $_GET["user_name"];

$sql="SELECT * FROM tbl_user WHERE user_name='$user_name'";
$result=mysql_query($sql);


$rows = array();

//retrieve and print every record
while($r = mysql_fetch_assoc($result)){
    // $rows[] = $r; has the same effect, without the superfluous data attribute
    $rows[] = array('data' => $r);
}

// now all the rows have been fetched, it can be encoded
//echo json_encode($rows);

$data = json_encode($rows);
echo $_GET['jsoncallback'] . '(' . $data . ');';
?>

i wondering if this method work or not? in my app, nth is display. i am not sure if the jsoncallback value is wrongly implemented. your opinions will be a great help. thanks

HUNG
  • 525
  • 7
  • 17
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 17 '13 at 23:06
  • The correct content-type for JSON-P is `application/javascript`, JSON-P is a JavaScript function call, not a data format. – Quentin Mar 17 '13 at 23:06
  • im confused what youre asking. trying to `jsoncallback` to the `displayStudent`? – VeXii Mar 17 '13 at 23:33

1 Answers1

0

The callback function should take three parameters:

data, textStatus, jqXHR

Where data is what is returned by the php page.

So your JS should be:

function start(){

    $('#detailsPage').live('pageshow', function(event) {
        var user_name = getUrlVars()["user_name"];
        //var user_name = "studentB";
        $.getJSON('http://mydomain.com/getStudent.php?user_name='+user_name, displayStudent);
    });
}

function displayStudent (data, textStatus, jqXHR) {
    // data will be the json encoded $rows data from your php file 
    // ... do stuff here
}

you PHP, I think (I haven't used PHP in 12 years...), just needs to be:

<?php 

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");    
header("Content-type: application/json");

include('mysqlConfig.php');

$user_name = $_GET["user_name"];

$sql="SELECT * FROM tbl_user WHERE user_name='$user_name'";
$result=mysql_query($sql);


$rows = array();

//retrieve and print every record
while($r = mysql_fetch_assoc($result)){
    // $rows[] = $r; has the same effect, without the superfluous data attribute
    $rows[] = array('data' => $r);
}

// now all the rows have been fetched, it can be encoded

echo json_encode($rows);
?>
scott.korin
  • 2,537
  • 2
  • 23
  • 36
  • in my previous question, because of some cross domain issue, i think to add jsoncallback to fix it. so i wondering should i delete it? http://stackoverflow.com/questions/15463296/how-to-display-json-content-in-li-format-in-phonegap – HUNG Mar 17 '13 at 23:13