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?
-
3You echo the password on screen?! Security problem.Raptor– Raptor2013年10月18日 07:42:03 +00:00Commented Oct 18, 2013 at 7:42
-
1This should work. Is the variable defined when you are using it?Yoav Kadosh– Yoav Kadosh2013年10月18日 07:42:23 +00:00Commented Oct 18, 2013 at 7:42
-
2Even worse: You store your password as plaintext?!Samuel– Samuel2013年10月18日 07:42:45 +00:00Commented Oct 18, 2013 at 7:42
-
3If 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– HamZa2013年10月18日 07:42:53 +00:00Commented Oct 18, 2013 at 7:42
-
1Yeah this should work.But i wonder if this actually satisfies your purpose of updating ? shouldn't you be using ajax or something ?Mevin Babu– Mevin Babu2013年10月18日 07:43:24 +00:00Commented Oct 18, 2013 at 7:43
4 Answers 4
change it to like.
document.getElementById('username').innerHTML=' Username: <input type="text" id="uname" name="uname" value="'+<?php echo $username; ?>+'">';
2 Comments
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
Comments
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.
1 Comment
<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);
}
4 Comments
.js file. Please check my answer.