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
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"
</script>
How can we do that?
Thanks
alert("<?= $msg ?>");
var strCurrUserName = "<?=$username?>";
var strCurrUserName = "<?=$username?>";
sorry..
just this
var strCurrUserName = "<?=$username?>";
just this
var strCurrUserName = "<?=$username?>";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that does it
ASKER
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>
<?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>
-r-