Reputation: 1282
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?
Upvotes: 1
Views: 8145
Reputation: 3855
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
Upvotes: 0
Reputation:
<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);
}
Upvotes: 0
Reputation: 2805
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.
Upvotes: 0
Reputation:
change it to like.
document.getElementById('username').innerHTML=' Username: <input type="text" id="uname" name="uname" value="'+<?php echo $username; ?>+'">';
Upvotes: 1