1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP menu - Does PHP have an onclick type of function?

Discussion in 'PHP' started by Vijizzle, Jun 13, 2006.

  1. #1
    Hey guys Im trying to hack together a website - this is my first time using php.
    Basically what Ive done is created a webpage in html but instead of adding the actual content of each page in the the html I have a little bit of php:

    <?php include("home.inc"); ?>

    which inserts the relevant content (of course for different pages I include different files).

    I was wondering if it was possible to have create a function where I had some variable instead of "home.inc" and when you clicked a link on the menu the variable would change and include the appropriate file - how would I do this.
    In javascript I could do this using the onlcick event - Im new to PHP so I have no idea if this can be done.

    Thanks in advance
     
    Vijizzle, Jun 13, 2006 IP
  2. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #2
    Are you trying to change that variable without refreshing the page? If so, that's not possible with PHP. Otherwise, you can either add a query string to your links to pass the variable onto the next page (i.e. page.php?fetch=somepage) or assign variables based on the referring webpage ($_SERVER['HTTP_REFERER'])
     
    sketch, Jun 13, 2006 IP
  3. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #3
    PHP is server side so you can't track browser events with it. Yuu could use AJAX to load the file but this would be useless if you want search engines to index it.

    The best way to construct php sites is to hard code the content of each page and then just include a common header and footer file.
    
    <?php
    inlcude("header.php");
    ?>
    
    write your content here
    
    <?php
    include("footer.php");
    ?>
    
    PHP:
     
    mad4, Jun 13, 2006 IP
  4. Djohn

    Djohn Peon

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not entirely sure this is what you're looking for, but I'll just go ahead and post it anyway.

    The PHP
    
    <?
    function content($page) {
         
         if(!$page) {
               $page = "frontpage";
               } 
    
         if(file_exists($page . ".php")) {
               include($page . ".php");
               } else {
               // If no file, use default
               include("frontpage.php");
               }
         }
    ?>
    
    PHP:
    This could be placed just about anywhere on your site, as long as it's included on the main page.

    Place this where the content should be included:
    <? content($_GET['page']) ?>
    PHP:
    Now you could make all the links you'd like, with a variable controlling the outcome:
    <a href="index.php?page=about">About the site</a>
    <a href="index.php?page=links">Links</a>
    HTML:
    What's happening is, the variable is passed on to the function, which then looks for a file by that name. If found, it's included and if not, the default frontpage is included.
    So, linking to "index.php?page=links" would include links.php, if it exists. You'd never have to mess with the actual include code again, just create files and links.

    Hope this helps...
     
    Djohn, Jun 13, 2006 IP
  5. Vijizzle

    Vijizzle Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Awsome thanks guys - yourve given me heaps to go - Ill have a play tonight and Ill see what I can come up with.
     
    Vijizzle, Jun 13, 2006 IP
  6. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #6
    The above way from mad4 is the best way to tackle it. Its simple and not much confusing :D
     
    Nick_Mayhem, Jun 14, 2006 IP