dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] Calling a PHP script from an "onclick" attribute?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Resolved [RESOLVED] Calling a PHP script from an "onclick" attribute?

    I have a web page that isn't working the way I need it to. My question is: Can you call a PHP function from an "onclick" attribute? Below is my code and nothing is happening when I click on a menu link. The variable $rootDir is defined at the top of my page before the <!doctype html> statment.

    Code:
        <body>
            <header>
                <div class="main">
                    <label style="width: 85%">Blake McKenna</label>
                    <label style="width: 85%" class="position">Senior Software Developer</label>
                    <img src="images/Me1.jpg" width="100" alt="Blake McKenna">
                    <div class="fakeHr"></div>
                    <a class="linkedIn" target="_blank" href="https://www.linkedIn.com/in/blake-mckenna">LinkedIn Profile</a>
    
                    <ul>
                        <li onclick="<?php checkMenuOption(1); ?>" <a class="menu" href="#">Summary</a></li>
                        <li onclick="<?php checkMenuOption(2); ?>" <a class="menu" href="#">Technical Skills</a></li>
                        <li onclick="<?php checkMenuOption(3); ?>" <a class="menu" href="#">Experience</a></li>
                        <li onclick="<?php checkMenuOption(4); ?>" <a class="menu" href="#">Education</a></li>
                        <li onclick="<?php checkMenuOption(5); ?>" <a class="menu" href="#">Certifications</a></li>
                        <li onclick="<?php checkMenuOption(6); ?>" <a class="menu" href="#">Contact Me</a></li>
                    </ul>
                </div>
            </header>
    
            <div id="frame" class="dframe">
                <?php 
                    function checkMenuOption($opt = 1) {
                        
                        echo "opt = " . $opt;
                        
                        switch($opt) {
                            case 1:
                                include $rootDir . "/pages/summary.php";
                            case 2:
                                include $rootDir . "/pages/skillset.php";
                            case 3:
                                include $rootDir . "/pages/experience.php";
                            case 4:
                                include $rootDir . "/pages/education.php";
                            case 5:
                                include $rootDir . "/pages/certifications.php";
                            case 6:
                                include $rootDir . "/pages/contact.php";
                        }
                    }
                ?>
            </div>
        </body>
    Not sure what I'm doing wrong. I event tried not using quotes like below:

    Code:
                    <ul>
                        <li onclick=<?php checkMenuOption(1); ?> <a class="menu" href="#">Summary</a></li>
                        <li onclick=<?php checkMenuOption(2); ?> <a class="menu" href="#">Technical Skills</a></li>
                        <li onclick=<?php checkMenuOption(3); ?> <a class="menu" href="#">Experience</a></li>
                        <li onclick=<?php checkMenuOption(4); ?> <a class="menu" href="#">Education</a></li>
                        <li onclick=<?php checkMenuOption(5); ?> <a class="menu" href="#">Certifications</a></li>
                        <li onclick=<?php checkMenuOption(6); ?> <a class="menu" href="#">Contact Me</a></li>
                    </ul>
    Blake

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,818

    Re: Calling a PHP script from an "onclick" attribute?

    No, you cannot. PHP is a server side script... not client side... onClick is used to invoke javaScript which is client side... now... you can use PHP on theh server to generate javascript that would then be executed on the client... but that doesn't look like what you are doing...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Re: Calling a PHP script from an "onclick" attribute?

    Using PHP, what would be the best way to show each of those *.php files? I know it's possible, I'm just not sure how to structure the code when a menu option is clicked.
    Blake

  4. #4
    Fanatic Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    900

    Re: Calling a PHP script from an "onclick" attribute?

    You will still need to use Javascript to get this done because the click is done client side. That will get sent back to the server so your PHP can deal with it. Here's one of many examples you can find.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,818

    Re: Calling a PHP script from an "onclick" attribute?

    I guess it depends on how you want to deal with it... for something like that, I'm not sure I'd use js... I'd just use the a href tags as intended, passing in the id through the url ... then the php reads it through _$GET and then it can include the appropriate content as needed.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Re: Calling a PHP script from an "onclick" attribute?

    Ok, you guys have given me some ideas. Thank you everyone!
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width