Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Actually, I have created a user profile page in which I have added a button to "update info" as for example:

<span id="username"> Username:  <?php echo $username; ?>
<span id="password"> Password:  <?php echo $password; ?>

In this page initially I have taken value of username and password using php/mysql commands from database.

Now, I have created a JavaScript file to change innerHTML of id username/password whenever I click "update info", I wanted to add something like as shown bellow:

function update()
{

document.getElementById('username').innerHTML=' Username:  <input type="text"    id="uname" name="uname" value="<?php echo $username; ?>">';

 }

But I am not getting value of username, I am just getting same php script within input text field.

How to do this in order to get value of "$username" which I was getting directly in main page?

share|improve this question
3  
You echo the password on screen?! Security problem. – Raptor Oct 18 '13 at 7:42
1  
This should work. Is the variable defined when you are using it? – Yoav Kadosh Oct 18 '13 at 7:42
2  
Even worse: You store your password as plaintext?! – Samuel Oct 18 '13 at 7:42
3  
If you have put that snippet in a JS file, then your server won't interpret it as a PHP file. Also the whole concept is wrong ... – HamZa Oct 18 '13 at 7:42
1  
Yeah this should work.But i wonder if this actually satisfies your purpose of updating ? shouldn't you be using ajax or something ? – Mevin Babu Oct 18 '13 at 7:43
up vote 0 down vote accepted

You can not wrote <?php ..... ?> in to your .js file. It won't be executed.

It must be in .PHP file only or else you can include script file in to your PHP file by include, require

share|improve this answer
1  

change it to like.

document.getElementById('username').innerHTML=' Username:  <input type="text"    id="uname" name="uname" value="'+<?php echo $username; ?>+'">';
share|improve this answer
    
nothing happening.. – Vivek Kumar Oct 18 '13 at 7:44
1  
Like @Hamza said, this wont work if the snippet is in a .js file. – Mevin Babu Oct 18 '13 at 7:45
    
@Mevin Babu you are write this is not working in .js file thank you Mevin. – user2050308 Oct 18 '13 at 8:01

It can't work, you can't write PHP code in JavaScript files.

You have to print the values inside the php page (.php extension), you can do something like:

<script>
 var username = "<?php print $username ?>";
 var password = "<?php print $password ?>";
</script>

but be sure you sanitize the PHP values or you expose your users to XSS attacks. Consider also avoiding to print the password, as rule. Usually when a user wants to change password he/she must provide the old one before the new.

share|improve this answer
1  
if JS code is in other file then how come it will work? – Ashish Oct 18 '13 at 7:50
    
Because the scope is window, that is global. Files organization doesn't matter at all. – Darko Romanov Oct 18 '13 at 7:51
<span id="username"> Username:  <?php echo $username; ?>
<input type="hidden" name="usernameHidden" id="usernameHidden" value="<?php echo $username; ?>"/>
<span id="password"> Password:  <?php echo $password; ?>

function update()
{
alert(document.getElementById("usernameHidden").value);
}
share|improve this answer
    
My code is already working if I am just putting JavaScript function in same file. So, doing all these stuffs would be worthless. – Vivek Kumar Oct 18 '13 at 7:57
    
ohh..why dont u add that value into session? – Ashish Oct 18 '13 at 7:58
    
no! Session is BAD!! – Darko Romanov Oct 18 '13 at 8:00
    
I have lot of things to add that is why I thought using session won't be good. My last option was Ajax. – Vivek Kumar Oct 18 '13 at 8:01
    
@VivekKumar You can not use PHP tags in .js file. Please check my answer. – Parixit Oct 18 '13 at 8:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.