0

This is simply a question that I cant seem to find answered. I have test.php included at the top of the main.php page. test.php contains jquery code with php inside of it and it is all wrapped in script tags. Am i able to include that file and use the jquery on that included file? Currently is not working but I may be doing something wrong. The question is am I able to do the include or do I have to have the script tags on the main.php page. Thanks Hope this makes sense.

         <?php  $query = mysql_query("SELECT * FROM table WHERE id = 1");
           $row = mysql_fetch_assoc($query);
            $result = $row['result'];


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

      var result = '<?php echo $result ?>';
   $(document).ready(function(e) {

 $("#div td:contains(" + result + ")").addClass('greenborder');

 });
  </script>
2
  • 1
    Please post the code that is not working. Commented Mar 21, 2013 at 19:52
  • 1
    what kind of include do you mean? PHP include or the HTML "include" Commented Mar 21, 2013 at 20:03

2 Answers 2

1

I see that you want to echo $result inside the javascript code.. and the way you are doing is just fine.. if you get nothing in place of $result on execution them maybe the variable $result is empty.. try var_dump($result); just after $result = $row['result']; in your code to see what it contains..

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

4 Comments

thanks for looking. I did that and get the string that I want to get. I just want to know if php mixed with jquery has got to be on the exact page that the code is running or if I cant include it from a different php file.
yes you can use php include() to include a php file in another php file (which might also contain javascript/jquery).. just make sure that you include the file before you use any of its variables.. you can only include a php file inside another .php file (not in a .js file)
ok thank you for the help... Ill keep plugging away at it and see what I come up with. Thanks
Figured it out. I had the include to low on the page. I thought I could put it anywhere but now have it at the top and now its working thanks man for the help
0

I'm assuming you have somethingl like this:

<?php
    $some_stuff = 'whatever';
?>
<html>

<head>
   <script>
        $(document).ready(function() {
                 <?php   some_php_code(); ?>
        });
etc...

If so, then ALL of the php code blocks will be executed by the server and will not make it to the client. You cannot embed raw php code inside a JS code block and have the PHP executed on the client. That'd require ALL of your site's users to have a full PHP install on their machines, plus whatever extra library's you're using, etc... e.g... never gonna happen.

Remember...PHP has no concept of other languages. It will not evaluate Javascript and only execute PHP blocks which match the JS conditions e.g.

<script>
if (true) {
   <?php code_for_true(); ?>  // PHP will execute this
} else {
   <?php code_for_false(); ?> // this will ALSO by executed by php
}

Both of those functions will be executed, because PHP has no concept at all of JS. Remember, there's no such thing as a PHP script "file". There's only OTHER files which have php code blocks embedded within them. PHP ignores everything except that which is surrounded by <?php ?> tags.

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.