dcsimg
Results 1 to 2 of 2

Thread: Calling a PHP function from "onclick"?

  1. #1

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

    Calling a PHP function from "onclick"?

    I have a web page that isn't working the way I need it to. My question is: I've researched into whether you 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. I don't know if I'm completely on the wrong path or if I just have some code in the wrong places.

    Code:
    <?php 
        $rootDir = "C:/apache/htdocs/resume/";
        $section = "pages/summary.php";
    ?>
    
    <!doctype html>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head lang="en">
            <title>Resume of Blake McKenna</title>
        </head>
    
        <body>
            <header>
                <div class="main">
                    <ul>
                        <li onclick="checkMenuOption(1)" class="initial" <a class="menu" href="#">Summary</a></li>
                        <li onclick="checkMenuOption(2)" class="initial" <a class="menu" href="#">Technical Skills</a></li>
                        <li onclick="checkMenuOption(3)" class="initial" <a class="menu" href="#">Experience</a></li>
                        <li onclick="checkMenuOption(4)" class="initial" <a class="menu" href="#">Education</a></li>
                        <li onclick="checkMenuOption(5)" class="initial" <a class="menu" href="#">Certifications</a></li>
                        <li onclick="checkMenuOption(6)" class="initial" <a class="menu" href="#">Contact Me</a></li>
                    </ul>
                </div>
            </header>
    
            <div class="sections">            
                <?php include $rootDir . $section; ?>
            </div>
            
            <script>
                function checkMenuOption(opt) {
                    switch(opt.tostring) {
                        case '1':
                            <?php $section = "pages/summary.php"; ?>
                            break;
                        case '2':
                            <?php $section = "pages/skillset.php"; ?>
                            break;
                        case '3':
                            <?php $section = 'pages/experience.php'; ?>
                            break;
                        case '4':
                            <?php $section = "pages/education.php"; ?>
                            break;
                        case '5':
                            <?php $section = "pages/certifications.php"; ?>
                            break;
                        case '6':
                            <?php $section = "pages/contact.php"; ?>
                            break;
                    }  
                }
            </script>
        </body>
    </html>
    Last edited by blakemckenna; Oct 4th, 2018 at 01:11 PM.

  2. #2
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,069

    Re: Calling a PHP function from "onclick"?

    Think of it this way - all the PHP on a page is already "executed" on the server, from which the output is then sent from the server to the client (your web browser). It's only after that, that your javascript even starts running in your browser. Your browser never gets any PHP.

    So really you can't click a link, and execute PHP the way you're thinking. For that you need to make a request back to the server using AJAX, API, or following a link to a new php page. It's a back and forth conversation between browser and server.

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