Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: PHP functions

  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts

    PHP functions

    why will this not work?

    Code:
    <?php
    function testing_function()
    {
    echo "THIS IS A TEST";
    }
    print "<input type='button' value='TEST' onclick='testing_function()'>";
    ?>

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,018
    Thanks
    2
    Thanked 313 Times in 305 Posts
    Php is parsed and executed on the web server at the time the page is requested. onclick events occur in the browser. You cannot "call" php functions from the browser. The browser can only make http requests to web servers (i.e. request pages through their URL.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    The onclick is looking for the JavaScript function named testing_function NOT the PHP function by that name.
    Dave .... HostMonster for all of your hosting needs

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Right. The following does work. But what I really want to do is save some session information. But I guess that is not possible in JavaScript, is it? Or can that be done using Ajax?

    Code:
    <script language='JavaScript'>
    function testing_function()
    {
    alert('test');
    }
    </script>
    <?php
    print "<input type='button' value='TEST' onclick='testing_function()';>";
    ?>

  • #5
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    It depends on how you want to handle your session information. In Javascript you can do it with cookies alright, but handling that stuff server side is probably cleaner.


  •  

    Posting Permissions

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