php variables / session variables do not get accessed in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aphadnis1234
    New Member
    • Jul 2013
    • 1

    php variables / session variables do not get accessed in javascript

    Hello All,
    I am developing a website With php as scripting Language.
    I have Applications in php scripts, html and JavaScripts.
    What I observe is that I can not Access the php variables
    those were executed on previous page With the JavaScripts on the NeXT page. Can some one help me resolve this issue.

    Below is an simple example :
    on one html page I have this:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     <html>
     <head>
     <title>Longon to Phadnis Domain</title>
     </head>
     <body>
     <p>Let wisdom prevail ...</p>
     <iframe src="main.php"></iframe>
           <form id="logged_user" method="post" action="getMainPHPForm(this);">
                 <input id="unam1" name="unam" type="text" value=""/>
           </form>
    
    
    </body>
     </html>
     
     <script type="text/javascript">
     
     var tim = '<?php echo time(); ?>';
     alert("Time:  " + tim);
    function getMainPHPForm (formId) {
        alert("formnew");
        var num = "<?=$_SESSION['uname'] ?>";
        alert("num:  " + num);
        //var un = document.getElementById("unam").value;
        var menuLst1 = document.getElementById("lst1");
        //alert("menulst :  " +  menuLst1);
        var lstStat = menuLst1.disabled;
        alert("lst_status  :  " +  lstStat);
        //document.getElementById("unam1").value="<?= $_SESSION['username'] ?>";
        //alert("Uform:  " );
    }
    </script>

    The main.php file has a session variable called username.
    This variable is not retriveble in the above JavaScript.

    I hope that some one can help me resolve this.
    Many thanks in advance.

    Ajeet Phadnis
    Last edited by Rabbit; Jul 23 '13, 07:54 PM. Reason: Please use code tags when posting code.
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #2
    i think instead of html you must change this page into .php, because when you are using <? ?>/<?php ?> /php tag it will never work. . . I hope you get me.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      @Exequiel: as of PHP 5.4 <?= will always work.

      The main.php file has a session variable called username.
      This variable is not retriveble in the above JavaScript.
      why should you have access to data from a completely isolated page? since main.php is embedded in an iframe, the outer page does not have access to its session cookie you need to share the session.
      Last edited by Dormilich; Jul 24 '13, 05:56 AM.

      Comment

      • Exequiel
        Contributor
        • Jul 2012
        • 288

        #4
        @Dormilich - <?php ?> tag will not work in html file, you need to make your file extension .html become .php
        you can use include'file.ph p'; if you want to access the variable(s) in file.php.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          <?php ?> tag will not work in html file, you need to make your file extension .html become .php
          just a matter of server configuration.

          Comment

          Working...