在使用jquery $.ajax或$.post时,要提交一个值(比如uid {user id}(,我如何定义一个回调函数来从PHP脚本中检索多个值,并从我的mysql表中检索值say(用户详细信息,对应于uid(并将它们存储在变量中,使用默认的数据类型:数据类型???
任何人都可以给我看一下jquery和php脚本吗?是的,数据库正在运行mySQL 5.5.24,PHP版本是5.3
instead of using $.ajax you can also use this...
var id=$("#id").val();
$.getJSON('script.php', {id:id}, function(json) {
var id=json.id;
var name=json.name;
var email=json.email;
});
}
in your php scrip..
<?php
mysql_connect($host,$dbuser,$dbpass);
mysql_select_db($db_name);
$id=$_GET['id'];
$query="select * from tbl_name where id='$id'";
$rs=mysql_query($query);
$row=mysql_fetch_assoc($rs);
$id=$row['id'];
$name=$row['name'];
$email=$row['email'];
$data=array("id"=>$id,"name"=>$name,"email"=>$email);
echo json_encode($data);
?>
最好的方法是将其作为 json 对象发送回 javascript。所以对于您的用户示例
// assuming post containing a username, using pseudo code for db access
$row = $db->fetchAssoc("select * from users where username = '".dbEscape($_POST["username"]."'");
header("Content Type: application/json");
echo json_encode($row);
ajax 代码如下所示
$.ajax({
type: "POST",
url: "your_page.php",
data: { param1: "val1", param2: "val2" }
}).done(function( data) {
// do your callback operation
});
从your_page.php中获取值,如下所示
$_POST["param1"] , $_POST["param2"]
如果您想了解更多信息,请单击此处
我认为这将解决您的问题。
<script type="text/javascript">
$.ajax({ // ajax call starts
url: 'serverside.php', // JQuery loads serverside.php
data: 'button=' + $(this).val(), // Send value of the clicked button
dataType: 'json', // Choosing a JSON datatype
success: function(data) // Variable data contains the data we get from serverside
{
}
});
</script>
dataType: json..so jason will return multiple values. from you php script
echo json_encode($array_of_val);
$.ajax({
type: "POST",
dataType:"html",
url: "ajax_page.php",
data:"params1="$params1"¶ms2="+$params2,
}).done(function(value) {
// write your callback operation
});
您可以使用 POST 方法获取ajax_page.php上的数据,例如
$_POST['参数1'];和$_POST['参数2'];
$query= select statement
$items=array();
while ($row = mysql_fetch_object($rs)) {
array_push($items, $row);
}
$result["rows"] = $items;
echo json_encode($result);