3

So I have this:

<?php
 echo '
  <script>
$(function(){
   $("a#yeah").click(function(){
           $.ajax({
        url: "ajax.php?action=yeah&id='.$id.'",
        success: function(html){
         $("a#yeah").html("your cool")
                   }
     })
   })


})</script>';

?>

basically I am using the PHP variable $id which can be find in the document, how could I get this same variable but without echoing the jQuery(so I could keep my editor syntax highlight in the JavaScript part)?

3 Answers 3

9

never echo any client side code - just type it as is.
PHP especially good in this http://www.php.net/manual/en/language.basic-syntax.phpmode.php

  <script>
$(function(){
   $("a#yeah").click(function(){
           $.ajax({
        url: "ajax.php?action=yeah&id=<?php echo $id?>",
        success: function(html){
         $("a#yeah").html("your cool")
                   }
     })
   })


})</script>
Sign up to request clarification or add additional context in comments.

1 Comment

So this actually work, never thought it worked as it is so strangely rendered in the editor, thanks.
6

You can add the php inline like:

<script> var yourVariable = '<?php echo $phpVar; ?>'; </script>

Comments

0

Just echo around the variable, as that appears to be the only piece requiring processing:

      ...stuff...
      url: "ajax.php?action=yeah&id=<?=$id?>",
      ...more stuff...

If your server doesn't have short_open_tag enabled, then <?php echo $id; ?>

Comments

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.