I am trying to inject a value stored in a PHP $_SESSION variable into the "value" attribute of an input tag using javascript. I am able to successfully inject the PHP code in the "value" attribute, but the stored value does not appear in the text box. My code is below:
HTML:
<input type="text" name="fname" value="" />
Javascript:
fields.eq(n).attr('value', "<?php echo $_SESSION['fname'] ; ?>");
The problem is that this would usually place the value of the $_SESSION['fname']
variable in the input box. But that is not the case. When the button is clicked to inject the PHP code into the value attribute, this is what the HTML looks like:
<input type="text" name="fname" value="<?php echo $_SESSION['fname'] ; ?>" />
And this is what I want it to look like but in the text box, it actually shows the PHP code and not the value stored in $_SESSION['fname']
.
Is there something I am missing? Shouldn't this work? Any help is appreciated!
PHP is a server-side script, which means: PHP will be executed on the server.
JavaScript is a client script, which means: JavaScript will be executed by the browser.
Usually by default, .php
files will be processed by the PHP engine on the server, thus executing the PHP scripts. The result will be sent to the client. That's why when you have a .php
file, things between <?php
and ?>
will be processed by the PHP engine and you will not see the actual PHP script in the client. Sometimes you can configure the HTTP server such that .html
files are also processed by the PHP engine.
For other files, the HTTP server (e.g. Apache) will directly sent the whole file to the client, without being processed by the PHP engine, so that any PHP scripts will not be processed and will directly go to the client.
Now your problem is that you tried to put some PHP into a JavaScript file (.js
), and hoping that it will be executed, but in fact it will not. So when you try to set the value of your input element with your shown JavaScript, the "text" <?php echo $_SESSION['fname'] ; ?>
will be set.
If you either put the JavaScript into your .php
file, or rename your JavaScript file (*.js
) to *.php
, you will get the value you desired.
Or probably you could add an extra JavaScript in your page, setting a "global" variable with your PHP, and use that "global variable" in your .js
file.
i.e. window.myFname = <?php echo $_SESSION['fname'] ; ?>;
Depending on what you want to do, you may need to choose how to design. If you just simply want to set the value, you may just simply do one of the two suggestions I've provided. If you want to get the value dynamically (i.e. obtain the value when you need it), you would like AJAX like others have suggested.
Php code is interpreted at request time so you will have to thing about the code you "inject" as a simple string as far as the browser/javascript is concerned.
You can do it like this, with a callback that returns the calue of the session var:
$('#someButton').click(function() {
$.ajax({
type: 'POST',
url: <callbackUrl>,
data: <params if any>
}).done(function( msg ) {
fields.eq(n).attr('value', msg);
});
});
Assume you have included the jQuery library.
getUser.php
<?php echo $_SESSION['fname']; ?>
For your JavaScript,
$(document).ready(function(){
$.get("getUser.php", function(data) {
fields.eq(n).attr('value', data);
});
});
For reference, http://api.jquery.com/jQuery.get/