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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.