开发者

how to get something back from a php file using jQuery?

开发者 https://www.devze.com 2023-02-23 07:29 出处:网络
i am sending some value to a php file like this: $.post(\'php/xxx.php\', { username: userName } ); and the php file echo\'es out a var $test

i am sending some value to a php file like this:

$.post('php/xxx.php', { username: userName } );

and the php file echo'es out a var $test

how can i get it back into the js?

i was thinking to use this:

var get_back = $.get('php/xxx.php', ... ); 

but i am not sure how...

thanks

edit:

here is my php file:

    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 FRO开发者_如何学PythonM database WHERE username='$username' LIMIT 1"));
    if($result !== FALSE) {
        echo $username;
    }

i want to get back this echo $username; value


The various ajax functions in jQuery (ajax, post, get) accept callback functions where you can handle the returned data:

//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;
       });
0

精彩评论

暂无评论...
验证码 换一张
取 消