8

Possible Duplicate:
Call PHP function from jQuery?

Jquery on button click call php function

$('.refresh').click(
    function(){
       <?php hello(); ?> 

    }
)

PHP Code

<?php
function hello()
{
echo "bye bye"
}
?>

I Want to call a php function from jquery on button click how can i do this ?

Community
  • 1
  • 1
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
  • 2
    Possible duplicate of [Call PHP function from jQuery?](http://stackoverflow.com/questions/3548802/call-php-function-from-jquery) and a thousand others (see the *Related* sidebar in this very question). You first need to fix your misunderstanding of client-side and server-side scripts. – deceze Apr 07 '11 at 09:32
  • do an ajax request to a php file and process the output the php file generated – ITroubs Apr 07 '11 at 09:32
  • @RobertPitt it is not impossible ;-) – ITroubs Apr 07 '11 at 09:34
  • but beware! this opens a gigantic door to calling functions that are not intended to be called – ITroubs Apr 07 '11 at 09:35
  • @deceze if the language he is using was not php but java this would be called RMI (Remote Method Invocation) where a client executes a function that is implemented on the server. – ITroubs Apr 07 '11 at 09:37
  • @ITroubs: Or more general: RPC. Depending on what the OP really wants, JSON-RPC might be interesting: http://en.wikipedia.org/wiki/JSON-RPC – Felix Kling Apr 07 '11 at 09:40
  • @ITroubs That's cute, but a) this is about PHP and b) behind the scenes Java would still use HTTP/AJAX or some other form of cross-network communication protocol. It doesn't invoke the server-side Java function locally just like that. :-P – deceze Apr 07 '11 at 09:41
  • 1
    @iTrubs, firstly thanks for the root access, secondly, the is server side, I was emphasizing the fact that its impossible to directly call PHP within the JavaScript engine! > `index.php?f=exec&arguments=rm -rf *` – RobertPitt Apr 07 '11 at 09:48
  • @deceze RMI doesn't do it either! it sends a request serialized request and gets the request in a serialized form. so actually doing RMI looks like you are invoking the function directly inside your code but actually the processing takes place on the remote server! – ITroubs Apr 07 '11 at 09:57
  • @RobertPitt like i said "but beware! ...." i know that this is a security issue. it was just to show that it might be done! – ITroubs Apr 07 '11 at 09:58
  • @ITroubs You can do the same thing in PHP/Javascript if you abstract the method calls a bit. As I said, behind the scenes it's the same thing. – deceze Apr 07 '11 at 10:06

4 Answers4

23

From jQuery you can only call php script with this function. Like that:

$.ajax({
   url: 'hello.php',
   success: function (response) {//response is value returned from php (for your example it's "bye bye"
     alert(response);
   }
});

hello.php

<?php
    echo "bye bye"
?>
Dmitry Evseev
  • 11,533
  • 3
  • 34
  • 48
8

your JS

$('.refresh').click(
    function(){
       $.ajax({
          url: "ajax.php&f=hello",
          type: "GET"
          success: function(data){
              //Do something here with the "data"
          }
       });

    }
)

your ajax.php

<?php

$validFunctions = array("hello","anotherF");

$functName = $_REQUEST['f'];
if(in_array($functName,$validFunctions))
{
    $$functName();
}else{
    echo "You don't have permission to call that function so back off!";
    exit();
}

function hello()
{
    echo "bye bye";
}

function anotherF()
{
    echo "the other funct";
}

function noTouch()
{
    echo "can't touch this!";
}
?>

this is a little example of really basic and pretty ugly RMI type invocation of php methods via ajax

demongolem
  • 9,474
  • 36
  • 90
  • 105
ITroubs
  • 11,094
  • 4
  • 27
  • 25
0

php is server side language, js - client side... I think you should use ajax. or your php function should return valid javascript code

Fivell
  • 11,829
  • 3
  • 61
  • 99
0

PHP is something that is executed on the server, the client (browser) has no direct access to any of the PHP code that is being executed. This is very nice because otherwise everyone could access all files and your entire mysql database.

You can combine php and javascript (jquery) with AJAX or a library like xajax.

gnur
  • 4,671
  • 2
  • 20
  • 33