我正在向 php 文件发送一些值,如下所示:
$.post('php/xxx.php', { username: userName } );
并且 php 文件回显出一个 var $test
如何将其返回到 js 中?
我想用这个:
var get_back = $.get('php/xxx.php', ... );
但我不知道如何...
谢谢
编辑:
这是我的 php 文件:
include("connect.php");
$get_username = $_POST['username'];
$username = '';
$username = mysql_real_escape_string($get_username);
echo $get_username;
$result = mysql_fetch_assoc(mysql_query("SELECT username FROM database WHERE username='$username' LIMIT 1"));
if($result !== FALSE) {
echo $username;
}
我想取回这个echo $username;
值
最佳答案
jQuery 中的各种 ajax 函数( ajax
、 post
、 get
)接受回调函数,您可以在其中处理返回的数据:
//define username outside so that
//it's available outside of the scope
//of the callback function
var username;
$.post('php/xxx.php',
{ username: userName },
function(data){
//per the comment below,
//assuming data will simply
//be the username
username = data;
});
关于php - 如何使用 jQuery 从 php 文件中获取某些内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5600855/