Forum Moderators: coopster

Message Too Old, No Replies

Calling javascript within php

what's the best way to do this?

         

skipfactor

6:59 pm on Aug 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm using the Google Maps API on a dynamic detail page. I want to serve one block of js based upon a database value, else serve a different block of javascript:

$foo=$row_table['col_name'];
if ($foo == 'blue')
{
echo '<script type=\"text/javascript\">
<!--

//-->
</script>';
}
else
echo '<script type=\"text/javascript\">
<!--

//-->
</script>';
?>

What's the best way to do this with a boatload of advanced API code that needs the database?

smatts9

2:04 am on Aug 21, 2006 (gmt 0)

10+ Year Member



You can just stop the script or something of that nature like this:


<?
$foo=$row_table['col_name'];
if ($foo == 'blue')
{
?>

<script type="text/javascript">
<!--
//-->
</script>

<?
} else {
?>

<script type="text/javascript">
<!--
//-->
</script>

<? }?>

Something like that should work.

skipfactor

6:00 pm on Aug 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's amazing what a few question marks can do for a guy's day. Thanks so much.

mojomike

7:30 pm on Aug 21, 2006 (gmt 0)

10+ Year Member



thanks to this post I have learned something.

I am just wondering what the ELSE section was for

Mike

coopster

5:32 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




I want to serve one block of js based upon a database value, else serve a different block of javascript:

I think the empty blocks are just being shown for example, mojomike.

BTW, PHP calls this Advanced escaping [php.net].

pixeltierra

4:58 am on Aug 23, 2006 (gmt 0)

10+ Year Member



If you have a lot of javascript, you could put it in separate files and include the one you want

if (X) {
include (jsfile.js);
}else{
include (theotherfile.js);
}

Seems cleaner

skipfactor

4:21 pm on Aug 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, I did that thanks, but for my situation I had to stuff the js in a php file:

if (X) {
include (jsfile.php);
}else{
include (theotherjsfile.php);
}