Hi All

I'm trying to call a php function from the onclick event of a button.
I will like to know the simples and easy way... but i miss something...
Right now i'm not use any form's

<?php 
function Test1(){
 echo "test1";
}

function Test2(){
 echo "test2";
}

...
<input type="button" name="EditBtn" value="Edit" class="form" onClick="Test1()">
<input type="button" name="DeleteBtn" value="Delete" class="form" onClick="Test2()">
...

?>


Hope someone can help
Nelson

    Onclick is an Java Script-Event. Java Script and PHP are two completly different things.

    Your onlick="test()" will start an java-script-Function test().

    It's not possible to call a PHP-Function with onclick (directly) without reloading/loading the page.

      so how can i use my php functions... should i use:

      <a href=... </a>

      or wish is the best option?

      only thing i want is when i press a link or button call the function...

      any idea?

      thanks
      and y english are good

        Answer #1: PHP operates on the server side. Javascript operates on the client side.

        Assume your web site is hosted in Chicago but your users come from all over the world like London, New York, and Paris. You write a file that is HTML mixed with some PHP. Then someone in London requests to see your web site. The first thing that happens is that the web server in Chicago reads your file from its hard drive. Then the web server looks at the PHP code and executes on the Chicago machine it which results in pure HTML. Finally, the Chicago machine sends the pure HTML (minus the PHP) to the user's machine in London.

        When the user in London gets the web page, there is no PHP in it so there's no way for the web page to trigger those PHP commands - they don't exist in the pure HTML file that they receive.

        You want to include some Javascript in the file. The HTML page that the user in London receives can have actions on buttons that display words when clicked. Since you are interested in displaying words when buttons are clicked, Javascript is probably the best language for you to be using.

        Answer #2: For more advanced programmers, Ajax might be the right solution.

        You can include Ajax code in your HTML file (the "j" in Ajax stands for Javascript so you're really just including Javascript). When you want the button click to effect some action on the Chicago web server, you can use Ajax so that the button click on the user's machine in London will send a message to the Chicago server that it should "do something".

        But if that "something" is to print "blah blah blah" on the user's screen, then it wouldn't make sense to use Ajax because Ajax is going to invoke some action on the Chicago machine.

        Therefore, simple Javascript (without Ajax) is probably the right tool for you to invoke an action on the user's machine - unless you have some need for the button click to make something happen on the web server.

          woooohh... really nice explanation, thanks etully.

          i understand the all the concept, i think the simple javascript its the best option but, i already have quite big functions in php, ... and to pass to javascript all the code...uff to difficulty for me.

          so i'm try other way, i'm not sure if works:

          <?      
          if($submit) { $sql = "INSERT INTO shop (productname,price,tablename) VALUES ('$productname','$price','$table')"; $result = mysql_query($sql); } ?> <form method="post" action=""> <input type="Submit" name="submit" value="Submit"> </form>

          some script that i found but don't work... its incomplete...

          any ideas about this example

          would be great,
          thanks in advance

            So what you want is for the user to click a button on the server side and then for some event to happen on the web server? If so, then you don't need Javascript. This is just a normal FORM.

            For example, if you want the user to be able to add items to a database on the web server, you would do it this way:

            Create two files: The first one (let's call it page1.html) is pure HTML like this:

            <form action="page2.php" method=post">
            Product Name: <input type=text name="productname"><br>
            Price: <input type=text name="price">
            <input type=submit value="Add this to the database on the web server">
            </form>

            Then you have a second file (called page2.php) that receives the data when the user clicks the submit button. The PHP inside this file will execute whenever the user clicks the button on their client side machine.

            The PHP in page2.php might look like this:

            <?php
            $productname=$POST['productname'];
            $price=$
            POST['price'];

                   $sql = "INSERT INTO shop  (productname,price) VALUES ('$productname','$price')";
                   $result = mysql_query($sql); 

            ?>
            <B>Thanks, we have added that item to the database on the web server</B>

            So you see, the HTML on the client's machine had a form action that pointed at a second script. The contents of the form were passed up to the web server (remotely) and the second script (page2.php) processed the PHP commands.

            The key is that the PHP commands run on the web server. You use this model whenever you want the user's click on their client machine to invoke some PHP action on the web server, not on the client's machine itself.

              everything works fine... the only problem is that when i insert this:

              action="<?php echo($_SERVER['PHP_SELF']); ?>"
              

              it jump to index.php, and not the php page it self that are in iframe...

              any ideia? :queasy:

                yes etully,

                only thing is i will like, when submit continue in same page...

                  The reason that I broke it up into two pages is so that you could see more clearly what is happenning. You can do it in one page - I just wanted it to be obvious that when the user clicks the button, the data is getting sent back to the web server for the PHP commands to execute.

                  So the way you build it all into one page is like this:

                  <?php
                  if ($POST['productname']) {
                  $productname=$
                  POST['productname'];
                  $price=$_POST['price'];

                  // Don't forget that you need to connect to the database before you can
                  // execute MySQL commands.

                  $sql = "INSERT INTO shop (productname,price) VALUES ('$productname','$price')";
                  $result = mysql_query($sql);

                  print "<B>Thanks, we have added that item to the database on the web server</B><p>";
                  }
                  ?>
                  <form action="page1.php" method=post">
                  Product Name: <input type=text name="productname"><br>
                  Price: <input type=text name="price">
                  <input type=submit value="Add this to the database on the web server">
                  </form>

                  This way, the form posts to the same page that generates the form. The PHP at the top only executes if it sees that data is arriving from the form. So the first time you pull up this page, the PHP will not execute but the form at the bottom will be displayed. When the user fills in the form and presses submit, it will send the data to the same page, the PHP will get the data, insert it into the database, and then display the form again.

                    By the way, this is important: The sample code that I gave you is just to show how you get all of this working on one page. I didn't make any effort to make the script safe and secure from hackers. You must learn about SQL injection before you use the code I showed you.

                      ye i see, i understand,

                      well it works... 🙂 the problem its solved

                      thanks alot etully and all the other for precious help 😉

                      Cheers,
                      Nelson

                        a year later

                        Those answer are good. But if we have several button. For example, a rating system where the user clicked buttons to rate articles.

                          ABHiker: I asked for your post to be moved to another thread, as it is a new issue & by a different poster.

                          I am not sure what you are asking..

                          When you have a rating system you either create
                          - Inidividual forms with their own rating value
                          - Use named buttons with values & hope that all the browsers pass the value
                          - Use text links to go to the processing page, where you pass the variables through the url ($_get syntax)
                          - Of course you can use AJAX to do this

                            4 years later

                            [FONT="Arial Black"][/FONT]

                            abhikerl;10872712 wrote:

                            Those answer are good. But if we have several button. For example, a rating system where the user clicked buttons to rate articles.

                            U can use isset method to see which button is selected and call the function based on the button selected..for eg..if ur button name is insert then in the new php file you can hve various loops like if(isset($button)=="insert") perform some action or else perform some other operation..

                            hope this would be helpful for u....

                              Write a Reply...