Unholy Prayer Posted March 15, 2007 Share Posted March 15, 2007 Is it possible? I want to include a list of database records in a dropdown list but I'm a little confused. Is it possible to include a php file in a javascript file? Quote Link to comment https://forums.phpfreaks.com/topic/42917-combining-php-and-javascript/ Share on other sites More sharing options...
slanton Posted March 15, 2007 Share Posted March 15, 2007 Like this? <?php $test ="Hullo"; ?> <script> var greeting ="<?php echo $test; ?>"; alert(greeting); </script> Quote Link to comment https://forums.phpfreaks.com/topic/42917-combining-php-and-javascript/#findComment-208445 Share on other sites More sharing options...
Unholy Prayer Posted March 15, 2007 Author Share Posted March 15, 2007 It would be like this: var menu1=new Array() <?php require_once('config.php'); while($sf=mysql_fetch_array($subforums)) { $sfid=$sf["sfid"]; $fid2=$sf["fid"]; $sfname=$sf["sfname"]; ?> menu1[0]='<a href=viewsubforum.php?sfid=$sfid'>$sfname</a>' <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42917-combining-php-and-javascript/#findComment-208470 Share on other sites More sharing options...
jcbarr Posted March 15, 2007 Share Posted March 15, 2007 Only thing I see wrong is that you would have to echo the variable in this line; menu1[0]='<a href=viewsubforum.php?sfid=<?php echo "$sfid'>$sfname</a>'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/42917-combining-php-and-javascript/#findComment-208477 Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 Nothing wrong with that as long as that file's extension is .php. If this is going to be a js file you can set the HEADER('content-type:text/javascript'); to make it appear like a .js file even though it has the .php extension =) <?php header("content-type:text/javascript"); // only required if you want to access file via <script src='etc'> ?> var menu1=new Array() <?php require_once('config.php'); while($sf=mysql_fetch_array($subforums)) { $sfid=$sf["sfid"]; $fid2=$sf["fid"]; $sfname=$sf["sfname"]; print "menu1[0]='<a href=viewsubforum.php?sfid=$sfid>$sfname</a>'"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42917-combining-php-and-javascript/#findComment-208484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.