简体   繁体   中英

How pass variable from php to jQuery

I have a config.php file where I define several variables.

I want to use some of those variables in a jQuery script.

I created a header-js.php file with the following content:

<script type="text/javascript">
jQuery("#msgid1").html("Hello world.");
jQuery("#msgid1").html("Hello world again.");
global_cookie_prefix = <?php echo(global_cookie_prefix);?>;
</script>

When I run the above the msgid1 div shows "Hello world again."

When I swap the lines to:

<script type="text/javascript">
jQuery("#msgid1").html("Hello world.");
global_cookie_prefix = <?php echo(global_cookie_prefix);?>;
jQuery("#msgid1").html("Hello world again.");
</script>

the msgid1 div shows "Hello world."

It seems that the line where I am defining global_cookie_prefix is causing the script to abort.

I am at a loss on how to solve this.

Thaks.

I'd bet the problem is that you're not defining your variable with the var keyword, and/or that your PHP snippet is not between quotes. Try this

var global_cookie_prefix = '<?php echo global_cookie_prefix; ?>';
^^^                        ^                                   ^

As you want to pass the variables to JS as strings, you will need to tell JS it's strings: global_cookie_prefix = '<?php echo global_cookie_prefix;?>'; (assuming global_cookie_prefix is a constant, in which case it should be all uppercase by convention)

are you sure global_cookie_prefix is a constant (because its not uppercase). Try adding the variable sign '$' to global_cookie_prefix in case its not a defined constant.

<?php echo($global_cookie_prefix);?>;

also you need quotes in your javascript... (try with and without $ depending on constant or not) btw by convention constants are uppercase.

global_cookie_prefix = "<?php echo global_cookie_prefix; ?>";

Since your script is actually a PHP script outputting Javascript, your script may be terminating on an error because global_cookie_prefix is an undefined constant. If global_cookie_prefix is a variable and you have it defined somewhere else on the page where this script is included, you might want to update your code to below by adding the '$' sign.

You also need the 'var' keyword in your JS to avoid errors in strict mode.

<script type="text/javascript">
  jQuery("#msgid1").html("Hello world.");
  **var** global_cookie_prefix = '<?php echo(**$**global_cookie_prefix);?>';
  jQuery("#msgid1").html("Hello world again.");
</script>

I would also recommend checking your apache error logs to see what the error and confirm that your PHP output is not breaking on that line.

Also, if you have error_reporting on, the PHP generated warning about global_cookie_prefix being undefined would definitely break your javascript and make it fail at that point.

It would be easier to help if you pasted more of your code to give this snippet context.

EDIT: I added single quotes around the PHP output since a string output from the PHP would break your JavaScript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM