1

wonder if someone could help me with a little problem. I have session values stored when my users login to my php site like this <?php echo $_SESSION['SecurityID_ContactName']; ?>.

I can reuse these values throughout the site to display the logged in users name, however I now need to store this value in an external js file which creates a form. This would then placed the logged in users name in a form field which is submitted back to mysql?

The form inside the js file has fields as such:

        form += '<label>Name</label>';
        form += '<input type="text" id="author" placeholder="Your name" />';

I edit this to hold the session as such:

        form += '<label>Name</label>';
        form += '<input type="text" id="author" value="<?php echo $_SESSION['SecurityAssist_ContactFullName']; ?>  />'";

This doesn't work, no real error as such its just removes the entire js script from the php page? Am I able to store the session value in a js file?

Kind regards

hakre
  • 193,403
  • 52
  • 435
  • 836
gary
  • 307
  • 8
  • 19

2 Answers2

2

You can still store it in your HTML, for example in a data- attribute on the body tag.

<body data-username="<?php echo $_SESSION['SecurityAssist_ContactFullName']; ?>">

You can query it in Javascript using getAttribute() for example:

var userName = document.body.getAttribute('data-username');
...
form += '<input type="text" id="author" value="' + userName + '";

Here is a small demo

For cross-browser support you can also use jQuery (attr() or data()) or check this question.


The other (IMO less clean) solution is to rename your Javascript file to .php. In this, case the webserver will run it, so you can use PHP commands inside. You should send the appropriate headers in this case (Content-type: text/javascript).

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
  • Hi there, many thanks for your answer, storing it in html has worked a treat, could this method also be used to add store other sessions? I also need the email session value inserting, is it possible to add this to the body tag and var in js? – gary Apr 11 '12 at 10:07
  • I dont know if its a really good idea to store the data in HTML attributes. You can easily print it out in JS – Dion Apr 11 '12 at 10:12
  • Old browser like IE-8 or older probably do not have those attributes (in HTML5) – Dion Apr 11 '12 at 10:14
  • Hmmm, yes I maybe should have checked before I got excited! FF & IE don't seem to like that, Chrome is ok though. I have been trying to print it out in js with no success, just removes the script from my php pages.. – gary Apr 11 '12 at 10:32
  • @DRP96 You can use `data-` attributes in every browser. Why not try it? `getAttribute()` might have some issues, but I included info about that. About storing data in HTML attributes: writing PHP variables into Javascript code simply makes me cry. Unmaintainable in the long run. – kapa Apr 11 '12 at 10:39
  • @gary Please create a jsFiddle showing what's not working and I will help. – kapa Apr 11 '12 at 10:39
  • @DRP96 just found http://stackoverflow.com/questions/2412947/do-html5-custom-data-attributes-work-in-ie-6 – kapa Apr 11 '12 at 10:55
  • 1
    Yes, of course it works in all browsers but it may cause an error or validating error because old browsers don't "know" this attribute. But actually it doesn't really matter, because the user would realize that. – Dion Apr 11 '12 at 10:58
  • @DRP96 The only downside is if you don't use the HTML5 doctype (which works in IE6!) your HTML won't be valid, but it will work fine. – kapa Apr 11 '12 at 11:03
  • Hi there sorry for delay and thanks for your interest. I have just redone the code and it does appear to work in IE, Chrome & FF, where I think I saw the mistake was once the form is submitted, it reloads itself as usual but the form field is no blank until you refresh the page. Thats not a great problem unless we have users trying to post lost of questions via the form.. Many thanks for your help on this much appreciated.. – gary Apr 12 '12 at 14:03
0

You simply have a wrong order of quotes; change it to:

form += '<input type="text" id="author" value="<?php echo $_SESSION['SecurityAssist_ContactFullName']; ?>" />';
Dion
  • 3,145
  • 20
  • 37
  • Hi there, thanks for your answer, I tried adding the above but that also removed the entire js script from php page, managed to get it working using the above posters solution. Thanks – gary Apr 11 '12 at 10:09