?
Solved

Using a php variable with javascript?

Posted on 2006-03-25
6
Medium Priority
?
243 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:minnirok
  • 2
  • 2
  • 2
6 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16289095
var strCurrUserName = "<?php echo addslashes(htmlspecialchars($username)); ?>";

-r-
0
 
LVL 33

Expert Comment

by:hongjun
ID: 16289104
alert("<?= $msg ?>");


var strCurrUserName = "<?=$username?>";
0
 
LVL 33

Expert Comment

by:hongjun
ID: 16289107
sorry..

just this

var strCurrUserName = "<?=$username?>";
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 16289133
hongjun:

date input: always validate;
data output: always escape;

-r-
0
 
LVL 7

Author Comment

by:minnirok
ID: 16294333
that does it
0
 
LVL 7

Author Comment

by:minnirok
ID: 16294356
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>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question