Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling php function from jquery? [duplicate]

Tags:

jquery

php

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 ?

like image 644
Vishwanath Dalvi Avatar asked Apr 07 '11 09:04

Vishwanath Dalvi


2 Answers

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"
?>
like image 69
Dmitry Evseev Avatar answered Oct 21 '22 19:10

Dmitry Evseev


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

like image 30
ITroubs Avatar answered Oct 21 '22 19:10

ITroubs