views:

410

answers:

6

I want to call a PHP function when pressing on a button, sort of like:

<?php 
function output(){
  // do something
}
?>

<input type="button" value="Enter" onclick="output()"/>

I tried to make something like:

<input type="button" value="Enter" onclick="test.php?execute=1"/>

where test.php is current page and then by php

<? if(isset(&execute)){ echo "Hello"; } ?>

but it doesn't work.

A: 

PHP is server side and javascript is client side. So I'm not sure if that is really what you want to be doing??

Perhaps you could explain why you want to specifically call a php function?

DaveJohnston
+1  A: 

Since PHP runs on the webserver, and buttons (and JavaScript in this case) appear on the client, you have to make an HTTP request to the server.

The easiest way to do this is to use a form. No JavaScript is required. You can add JavaScript (although it should be layered on top of a working non-JS version). Using JavaScript to make an HTTP request without leaving the page is known as Ajax, and generally achieved with the XMLHttpRequest object. There are various libraries such as YUI and jQuery that can do some of the heavy lifting for you.

David Dorward
A: 

I googled PHP function from button and found this question on webdeveloper.com

It doesn't use Javascript.

pavium
A: 

This is PHP you're talking about, not ASP.NET. In PHP, there is no such thing as a button click event. PHP runs entirely on the server and has absolutely no knowledge of client-side events.

Your first try won't work because the PHP code only runs when the page first loads. It does not run when you call a JavaScript function. Your second example won't work because JavaScript and PHP can't talk directly to eachother like that. Trying to directly call a PHP function from JavaScript just doens't make sense. Remember, PHP only runs on the server. By the time you get to the point where JavaScript can run, the PHP code has long since completed its work.

If you want to do something when a button is clicked, you have to explicitly make a request back to the server. You can do this by just POSTing the form as CTphpnwb suggested. Just be aware that this will reload the page and you will have to manually save and restore the page state, e.g. repopulate input boxes. There is no built-in magic that will do this for you.

Alternatively, you can get all AJAXy and do the POST in JavaScript. However, you will have to write the JavaScript to send the request and process the response, and write the server-side PHP code to handle the request. This gets a little awkward to do in a single page.

From : http://www.dreamincode.net/forums/showtopic72353.htm

Braveyard
Why was this voted down? It says similar things to the other posts only in slight more details
John Burton
Some people are weird creatures. That's what I know now.
Braveyard
+1  A: 

I think using an AJAX call would do sort of what you are asking. I don't know PHP very well but you can use the following example, and add another variable with the data you are passing in to the server to indicate which function you want to call on the server. On the server you can add some "IF" statements that will call a certain function based on the name passed in and return the result.

Here is what you could use on in your javascript client using the jQuery library as a helper to do the AJAX call:

    <input type="button" value="Enter" onclick="output()"/>
    <script type="text/javascript">
function output(){
 $.ajax({
  type: "POST",
  url: "submit_data.php",
  data:  "username=" + "SomeUser" 
  + "&email=" + "[email protected]"
                    +       "&functionName=" + "theFunction1",
  success: function(html){
   alert('sucess!  Result is:' + html);
  }
 });

    }   
</script>

and you can use code such as this to catch the data your javascript is passing in. In this example you would want to call this file name as "submit_data.php" to match the javascript above:

<?php

    // Variables

    $Username = $_POST['username'];
    $Email    = $_POST['email'];    
    $FunctionName = $_POST['functionName'];

    //Add code here to choose what function to call and echo the result
    // If $FunctionName equals 'theFunction1' then execute theFunction1
    // If $FunctionName equals 'theFunction2' then execute theFunction2

    echo "You called A Page!";

?>

Here I am doing nothing with the "username" and "email" simply grabbing it and storing them into holding variables. But you can easily add extra functionality here, such as checking for a name of a function that you want to run.

Roberto Sebestyen
I like your answer but it needs to be updated to point out that you are using the jQuery library.
ChaosPandion
Thanks... Updating now
Roberto Sebestyen
A: 

Hi,

You cannot directly invoke a PHP function from Javascript this way :

  • PHP code is executed on the server
  • HTML / Javascript are interpreted on the client-side.

One the HTML page has been generated and sent to the client (the browser), there is nothing more PHP can do.

One solution would be to use an Ajax request :

  • Your onclick event would call a Javascript function
  • This Javascript function would launch an Ajax request : a request sent to the server
  • The server would then execute some PHP code
  • And, then, return the result of that execution to the client
  • And you'd be able to get that result in your Javascript code, and act depending on what was returned by the server.

There are plenty of solutions to do an Ajax request :

  • You can re-invent the wheel ; not that complex, I should say -- but see the next point
  • If already using a Javascript framework, like jQuery, Prototype, ... Those provide classes/methods/functions to do Ajax requests

Googling a bit will get you lots of tutorials/examples, about that ;-)

Pascal MARTIN