我正在向这样的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;
值
答案 0 :(得分:6)
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;
});