Reputation: 5201
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>
Upvotes: 0
Views: 465
Reputation: 6878
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..
Upvotes: 1
Reputation: 360602
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.
Upvotes: 0