Patrioticcow
Patrioticcow

Reputation: 27038

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

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 FROM database WHERE username='$username' LIMIT 1"));
    if($result !== FALSE) {
        echo $username;
    }

i want to get back this echo $username; value

Upvotes: 1

Views: 738

Answers (1)

no.good.at.coding
no.good.at.coding

Reputation: 20371

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

Upvotes: 6

Related Questions