Jump to content

php and javascript variable integration?


Hall of Famer

Recommended Posts

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();      

Link to comment
https://forums.phpfreaks.com/topic/253356-php-and-javascript-variable-integration/
Share on other sites

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.

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".

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.