9

Is there anyway I can use a php variable in the JQuery script?

Example:

  • PHP variable: $sr2
  • Excerpt of JQuery script (with variable): $('#a2_bottom_$sr2')

How can I make it so the variable is valid in that JQuery part?

Thanks

2
  • 3
    Where is your jQuery code located? Inside the PHP file or in a separate .js file? Commented May 19, 2012 at 19:40
  • 1
    It is located in the same file as the PHP Commented May 19, 2012 at 20:12

4 Answers 4

11

PHP runs on the server, jquery runs on the client. If you want a PHP variable to be available to jquery (and by extension, the underlying javascript engine), you'll have to either send the variable's value over at the time you output the page on the server, e.g.

<script type="text/javascript">
    var my_php_var = <?php echo json_encode($the_php_var) ?>;
</script>

or retrieve the value via an AJAX call, which means you're basically creating a webservice.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for json_encode; I think it's crucial that if we're going to discuss printing a variable as JS code that we discuss how to avoid breaking it :)
9

What you could simply do is use your PHP to echo out the code to initiate a JavaScript variable.

<script type="text/javascript">
<?php

  $phpVar = "foo";
  echo "var phpVariable = '{$phpVar}';";

?>
</script>

Once the PHP code is parsed, and the HTML is sent to the user - all they will see is the result of the PHP echo -

<script type="text/javascript">
  var phpVariable = 'foo';
</script>

Now your phpVariable is available to your JavaScript! So you use it like you would in any other case -

$("div."+phpVariable);

That will retrieve us any <div> element with a foo class -

<div class="foo"></div>

2 Comments

Its Really old but now I'm getting javascript Error in your code "Uncaught SyntaxError: Unexpected token < "
@IrtzaShahan - this means that there was a mistake when inserting the variable into the javascript. Make sure that you wrap your php variable with quotes so that the end result that you echo onto the page will look like normal javascript. Remember that the browser still needs to read and use the javascript values that you created.
5

Assuming your jQuery is in the same file:

... $('#a2_bottom_<?php echo $sr2 ?>') ...

3 Comments

works fine... unless $sr2 contains quote character, space, etc.
But since it is part of an ID, it can't contain those characters anyway.
exactly; if the $sr2 variable contains any of those characters, whether by accident, or by a deliberate hack attack, it will make the line of jQuery code invalid. At the very least, use json_encode() to ensure it's valid syntax, but in this case further validation may also be required such as stripping spaces, etc.
5

You could output it as part of the page in a script tag... i.e.

<script type="text/javascript">
<?php
echo "var sr2 = \"" . $sr2 . "\"";
?>
</script>

Then your jQuery line would be able to access it:

$('#a2_bottom_' + sr2)

1 Comment

for safety, use json_encode() when echoing the variable to your javascript, but basically this is the right answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.