php – 如何使用jquery ajax提供和获取数据?
发布时间:2020-12-13 21:50:38 所属栏目:PHP教程 来源:网络整理
导读:我创建了一个 jquery ajax请求,并尝试将一个变量传递给php来进行查询并将数据返回给实际的上下文. 处理ajax的实际上下文: $.ajax({ type: 'post',url: 'show.php',data: {name: name},dataType: 'json',success: function(response) { //here I'd like back
我创建了一个
jquery ajax请求,并尝试将一个变量传递给php来进行查询并将数据返回给实际的上下文.
处理ajax的实际上下文: $.ajax({ type: 'post',url: 'show.php',data: {name: name},dataType: 'json',success: function(response) { //here I'd like back the php query } PHP: $hostelName = $_POST['name']; $sql = //here is the actual sql containing the $hostelname $query = mysql_query($sql); $obj = mysql_fetch_object($query); $sum = $obj->sum; $tour = $obj->tour; echo json_encode( array( "sum" => $sum,"tour" => $tour ) ); 解决方法
试试这个:
$.ajax({ type: 'post',data: "name="+ name,success: function(response) { //here I'd like back the php query } 和你的PHP代码: $hostelName =mysql_escape_string($_POST['name']); $sql = //here is the actual sql containing the $hostelname $query = mysql_query($sql); $reusult = mysql_fetch_assoc($query); echo json_encode($reusult); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |