-1

Currently, I have a Javascript file (running on the client) that I call a PHP file(on the server) from. After the PHP file finishes, I would like to pass three variables back to the javascript that is running on the client. I figured out how to make the javascript wait for the php file for finish executing, so that is not the problem. The problem is passing the variables back to the javascript file. Can this be done? All the examples that I have seen have has some sort of hybrid javascript/php file. I was hoping to find some way to pass the php variables kind of like the jquery.ajax does. I don't know if this can be done because I am so new to Javascript and PHP. Thanks,

javascript:

<html>
    <head>
        <title>Login Page</title>
    </head>
    <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script language="Javascript">
        function calculations(callback)
        {
            var x = <?php echo $username?>;
            alert(x);
            var password = window.prompt("Please Type Your Password");
        }//ends the calculations function
        function getUsername()
        {
            var username = window.prompt('Please Type Your Username');
            var temp = document.getElementById('temp');
            temp.innerHTML = username;
            jQuery.ajax(
                {
                    type: "POST",
                    url:"GetInfo.php",
                    data: "username="+username,
                    success: function(msg)
                            {callback.call();}

                });//ends the jQuery send


        }//ends the GetUsername function
    </script>
    <body onLoad=getUsername()>
        <div id="temp">This will show text</div>

    <body>

</html>

php:

<?
//$username = $_POST['username'];
$username = "tom";
$inFile="MyID.config.php";
$handle=fopen($inFile, 'r') or die ("No credentials could be gotten because the file MyID.config.php would not open.");
$data='0';

do{
$data = fgets($handle);
$temp = substr($data,0, 10);
//echo $temp.strcmp($temp,'\'username\'')."\n";
}while (strcmp($temp, '\'username\'')!= 0);


$data = substr($data,15,strlen($username));

if (strcmp($data, $username == 0) )
{
    $read_in = fgets($handle);
    $x = substr($read_in,8,-3);
    $read_in = fgets($handle);
    $y = substr($read_in,8,-3);
    $read_in = fgets($handle);
    $salt = substr($read_in,11,-3);
}//ends the strcmp $data and $username if statement.
fclose($handle);

?>
tpar44
  • 1,431
  • 4
  • 22
  • 35
  • Seems like a duplicate... http://stackoverflow.com/questions/1600360/passing-multiple-parameter-to-php-from-javascript – Edwin Apr 21 '12 at 02:00
  • Would also add that you should check out JS Arrays and JSON, perhaps through PHP's `json_encode` method. – Edwin Apr 21 '12 at 02:01
  • @Edwin im pretty sure that the link you gave is the opposite the person is looking to send javascript variables to a php page. The poster should have used ajax. I will definitely look into the other stuff that you said though. – tpar44 Apr 21 '12 at 02:41

1 Answers1

1

To pass data back to the Javascript that called the script, just echo like this:

$value = 'foobar';
echo $value;

For multiple values, you could pass it back as a JSOn object

$name = 'foobar';
$text = 'helloworld';
$value = 5;

//add to associative array

$result['name'] = $name;
$result['text'] = $text;
$result['value'] = $value;

// encode as json and echo
echo json_encode($result);

On the Javascript side, your callback function would receive this:

function callback(data){
    var result = JSON.parse(data);
    //access the members
    console.log(result.name);
    console.log(result.text);
    console.log(result.value);
}
Ayush
  • 41,754
  • 51
  • 164
  • 239
  • I tried your answer but there is an error telling me that an unexpected character is encountered in the statement `result = JSON.parse(data);` – tpar44 Apr 21 '12 at 02:37
  • In the above comment I meant `var result = JSON.parse(data);` – tpar44 Apr 21 '12 at 02:56
  • 1
    Sry I got it now. I was putting the `JSON.parse(data);` in the wrong function. By accident, the `callback` function was the jquery function instead of the one before it. – tpar44 Apr 21 '12 at 03:15
  • 1
    Note that in Internet Explorer JSON object is available since IE 8. If you care for IE7 you could use jQuery's jQuery.parseJSON as a replacement. – wdev Apr 21 '12 at 15:01