SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Write function in php executing with a button without having to post method to site

    I can write a function in php.

    How do I call it with a JavaScript Button in the same website without having to go to another php site?

    Code:
    <?php
        function fn1() {
              ...code
        }
    ?>
    <script type="text/javascript">
        function javascriptbtn()
              ???
        }
    </script>
    
    <form name="myform">
    <input type="button" name="Button" value="Calculate Julian Date" onclick="javascriptbtn()" />
    </form>

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    413
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Are you wanting it to execute without calling back to the server? Isn't PHP a server-side language, so that's not going to happen? (Learning here, so might be wrong).
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Droopsnoot is right, PHP is executed on the server so there's no way to run additional PHP code after the page has loaded without making another request to the server.

    You've basically got two options:
    1. Use JS to make an AJAX call to the server, to get the results of a PHP script without having to reload the page.
    2. Write the functionality you want purely in JS.
    "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies."


Bookmarks

Posting Permissions

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