Link to home
Start Free TrialLog in
Avatar of minnirok
minnirok

asked on

Using a php variable with javascript?

Hi,

I'm following a book about php sessions. So I've set myself up to the point where a user on my system can login and is served a page that looks like this:

<?php

    session_start();
    $username = $HTTP_POST_VARS['username'];
    $password = $HTTP_POST_VARS['password'];
?>

Ok so that is great, now the page loads and knows the logged in username and password. I have some javascript functions that need to know the username though at some point:

<script>
    var strCurrUserName = $username;
    var vParams = strCurrUserName + "," + "something good";
    httpAjaxRequest.open("GET", url + vParams, true);
</script>

How can we do that?

Thanks
Avatar of Roonaan
Roonaan
Flag of Netherlands image

var strCurrUserName = "<?php echo addslashes(htmlspecialchars($username)); ?>";

-r-
alert("<?= $msg ?>");


var strCurrUserName = "<?=$username?>";
sorry..

just this

var strCurrUserName = "<?=$username?>";
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of minnirok
minnirok

ASKER

that does it
This question was for 500 points, may I ask one more related question that is also pretty simple? I'm using some AJAX in my script, but the php function I'm calling always returns an extra space character:

<?php
    echo "Hello!";
?>

<script>

function waitForAjaxResponse()
{
    if (httpResponse.readyState == 4) {
        alert("["  + httpResponse.text +  "]");  
    }
}

And the alert box always says:

    "[ Hello!]"

Any ideas why the heck I'm always getting that extra space character in there?

Thanks
</script>