-1

I am new to javascript and trying to send parameter from php to js file. I am trying to send parameter from php file to js file but i dont know why this is not happening.

code:

javascript file name: data6_1.js

var menuItems = [
    ["Collected Material","scm.php?firstname=.$id2", "", "", "", "", "0", "0", "", "", "", ],
];

PHP file: studentbook.php

<?php                       
    if(!isset($_SESSION['firstname'])) {
        $id2 =$_REQUEST['firstname'];
        echo "<b>".$id2."</b>"." Logged in";
    } 
?>

<div id="MNUOBJ7DFA52C14221AD2" style="position:absolute; top:549px; left:22px; width:174px; height:34px; z-index:3016;">
    <script type="text/javascript" src="data6_1.js"></script> 
    <script type="text/javascript">
        menuItems.([$id2]);
    </script>
    <a href='scm.php'></a>
</div>

Any idea please help.

cl3m
  • 2,791
  • 19
  • 21
Malik
  • 193
  • 3
  • 12
  • the php inside the javascript file will not be executed as php, it will be treated as a string. Also, you appear to use a php variable directly in the javascript ( `menuitems.( [$id2] );` ) ~ that also will not work as you expect ~ that should be enclosed in php tags – Professor Abronsius Feb 13 '16 at 08:16
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – John Dvorak Feb 13 '16 at 08:49

1 Answers1

1

When your browser requests the file data6_1.js there is absolutely no php involved in your processing of that request. It is a simple request to a javascript file and that's it. How should the $id2 magically turn into some value it might have inside some other script execution that is not connected at all? You could have that javascript created in a dynamic manner by means of php, but that does not solve your issue at hand, since that would _still be two totally separate script executions that will not share any variables just like that.

Instead you could pass over that value as passive value inside the markup: <input type="hidden" value="<?php echo $id2; ?>">. Then you can simply select the value in javascript and use it.


In the lower code portion of your question you need to use php to output the value of $id2, otherwise it will be a literal in the html portion of your php script:

<?php    
    if(!isset($_SESSION['firstname'])) {
        $id2 =$_REQUEST['firstname'];
        echo "<b>".$id2."</b>"." Logged in";
    } 
?>

<div id="MNUOBJ7DFA52C14221AD2" 
     style=" position:absolute; top:549px; left:22px; width:174px; height:34px;  z-index:3016;">
    <script type="text/javascript" src="data6_1.js"></script> 
    <script type="text/javascript">
        menuItems.([<?php echo $id2; ?>]);
    </script>
    <a href='scm.php'</a>
</div>

A possible alternative would be to dynamically pack only a single javascript element:

<?php    
    if(!isset($_SESSION['firstname'])) {
        $id2 =$_REQUEST['firstname'];
        echo "<b>".$id2."</b>"." Logged in";
    } 
?>

<div id="MNUOBJ7DFA52C14221AD2" 
     style=" position:absolute; top:549px; left:22px; width:174px; height:34px;  z-index:3016;">
    <script type="text/javascript" src="data6_1.js">
        var menuItems = [
            [
                "Collected Material",
                "scm.php?firstname=<?php echo $id2; ?>", 
                "", "", "", "", "0", "0", "", "", "", 
            ],
        ];
        menuItems.([<?php echo $id2; ?>]);
    </script>
    <a href='scm.php'</a>
</div>

You may be able to shorten <?php echo $id2; ?> to <?= $id2 ?> depending on the setup of your php engine. That variant is much more compact and readable.

And don't forget that may have to adjust your "content security policy" header to allow the execution of inline scripts: http://content-security-policy.com/

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • thanks but i am still getting $id2 as output. is my js parameters are correct? – Malik Feb 13 '16 at 08:23
  • i mean this one: var menuItems = [ ["Collected Material","scm.php?firstname=.$id2", "", "", "", "", "0", "0", "", "", "", ], ]; – Malik Feb 13 '16 at 08:23
  • OK, done, a bit lengthy, but I think you are able to follow my thoughts and get an idea... – arkascha Feb 13 '16 at 08:43
  • 1
    What I still don't udnerstand is what you try to express by `menuItems.([]);`... – arkascha Feb 13 '16 at 08:44
  • I wish people would stop answering and upvoting obvious duplicates like this one... – John Dvorak Feb 13 '16 at 08:50
  • @arkascha you would have helped the asker - and the site - much more by just pointing the asker to the duplicate. A golden badge in the javascript tag even lets you do that without consent of four other people, so you don't even risk reduced visibility. It would also cost you less time. – John Dvorak Feb 13 '16 at 08:52
  • thanks arkascha the reason for that menuItems.([]); is that i am calling "menuItems" variable which is in java. i dont know if it is a correct or not? tutorial: http://feather.elektrum.org/book/src.html – Malik Feb 13 '16 at 08:58
  • i thought like object oriented programming this may be possible – Malik Feb 13 '16 at 08:59
  • i am having a class i will come later for discussion – Malik Feb 13 '16 at 09:01
  • If you don't think the question should be closed as duplicate, why shouldn't it be? – John Dvorak Feb 13 '16 at 09:03
  • @Malik Sorry, but I do not see `java` involved here... Java and javascript are not related _at all_. Note that with `var menuItems = ...` you decleare menuItems _as a variable_. You cannot "call" a variable. – arkascha Feb 13 '16 at 09:05