Hall of Famer Posted December 17, 2011 Share Posted December 17, 2011 Well this is the code I currently have, it is derived from ajax organic tabs codes. <? if($filename == "profile"){ ?> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script> <script src="js/tabs.js"></script> <script> $(function() { $("#profile").organicTabs(); }); </script> <? } ?> It may look fine this way, but rather inflexible if I wish to implement more tabs in future instead of just for the profile page. Assuming I have profile.php, members.php and stats.php and all of the three script files use tab system, the codes will become: <? if($filename == "profile"){ ?> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script> <script src="js/tabs.js"></script> <script> $(function() { $("#profile").organicTabs(); }); </script> <? } elseif($filename == "members"){ ?> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script> <script src="js/tabs.js"></script> <script> $(function() { $("#members").organicTabs(); }); </script> <? } elseif($filename == "stats"){ ?> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script> <script src="js/tabs.js"></script> <script> $(function() { $("#stats").organicTabs(); }); </script> <? } ?> Which can get even more tedious as more and more pages needs to adopt tabs system. I wonder if there is a way to simplify the code by passing the php variable $filename directly into javascript tabs definition in this line, where the part $("#profile") changes as filename changes? Please help... $("#profile").organicTabs(); Quote Link to comment https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/ Share on other sites More sharing options...
trq Posted December 17, 2011 Share Posted December 17, 2011 $("#<?php echo $filename; ?>").organicTabs(); Quote Link to comment https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/#findComment-1298738 Share on other sites More sharing options...
Hall of Famer Posted December 17, 2011 Author Share Posted December 17, 2011 Thanks a lot for your rapid reply, but I dont understand why you include an echo in the code. In my script echo cannot be used anywhere inside the php codes like that since it will mess up the css of the theme. Quote Link to comment https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/#findComment-1298754 Share on other sites More sharing options...
jcbones Posted December 17, 2011 Share Posted December 17, 2011 ??? How does an echo mess up a theme? An echo writes data to a browser, if it messes up a theme, it is because PHP has been told to send the wrong data to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/#findComment-1298865 Share on other sites More sharing options...
Hall of Famer Posted December 18, 2011 Author Share Posted December 18, 2011 Well in my script everything that needs to be printed to the browser will be stored in a variable $article_content, and eventually the following function will be called to print everything out at once: echo showpage($article_title, $article_content, $date); So yeah, echo can only be used at the very end of the script file. Calling additional echo will mess up the template, sadly. Quote Link to comment https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/#findComment-1298970 Share on other sites More sharing options...
trq Posted December 18, 2011 Share Posted December 18, 2011 Sorry but that response makes little sense. If you want the value of a php variable to be outputted to your page you must echo it. Hence: $("#<?php echo $filename; ?>").organicTabs(); Would output: $("#profile").organicTabs(); If your php variable $filename contained the string "profile". Quote Link to comment https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/#findComment-1298976 Share on other sites More sharing options...
Hall of Famer Posted December 18, 2011 Author Share Posted December 18, 2011 Well it is stored as a variable, not to be output directly to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/#findComment-1298978 Share on other sites More sharing options...
trq Posted December 18, 2011 Share Posted December 18, 2011 Then how exactly do you plan on getting it into JavaScript? Quote Link to comment https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/#findComment-1299059 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.