Vaibhav Jain
Vaibhav Jain

Reputation: 505

i want to Call Php user define function from jquery ajax

I want to call php user define function from jquery ajax( $.ajax({}) )

my ajax code is in index.php and php user define function is in functions.php

both are in same folder

this is my index.php code

<html>
<head>
<script src="headerfiles/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function()
{
        $("#display").click(function()
        {
                var mobile=$("#mobile").val();
                $.ajax({
                method:"post",
                url:"functions.php",
                success:function(name){alert(name);}
                });
        });
});

</script>

</head>
</body>
<input type="text" id="mobile" name="mobile" />
<input type="button" id="display" name="display" value="Display" />
</body>
</html>

and functions.php code is

function fetch_name($mobile)
{      
    $name="my query............"
    echo $name;
    //or
    return $name;
}

I want to display name in index.php page

Upvotes: 0

Views: 739

Answers (3)

Roopendra
Roopendra

Reputation: 7776

As per your script Html :-

<html>
<head>
<script src="headerfiles/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function() {

    $("#display").click(function(e)
    {
        var postData = $('#mobile').val(); // Data which you may pass.
        var formURL = 'function.php'; // Write callback script url here
        $.ajax(
        {
        url : formURL,
        type: "POST",
        data : postData,
        success:function(data, textStatus, jqXHR)
        {
            alert(data);
            //data: return data from server
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            //if fails     
        }
        });
    });
});
</script>

</head>
</body>
<input type="text" id="mobile" name="mobile" />
<input type="button" id="display" name="display" value="Display" />
</body>
</html>

In function.php :-

<?php
    // Post Value
    $mobile = isset($_POST['mobile']) ? $_POST['mobile'] : '';

    fetch_name($mobile);

    function fetch_name($mobile) {
      echo $mobile;
     // Your function body goes here.
       exit;
    }

?>

Upvotes: 0

Ramz
Ramz

Reputation: 326

you can do this

in js add:

data:{fc : 'fetch_name'};

in php

$fc = $_POST['fc'];
$fc();

function fetch_name($mobile)
{      
  $name="my query............"
  echo $name;
  //or
  return $name;
}

Upvotes: 2

Eswar Rajesh Pinapala
Eswar Rajesh Pinapala

Reputation: 4911

//In your ajax send a post param 


$.ajax({
method:"post",
url:"functions.php",
data: { 
        'foo': 'function_name', 
    },
............
.................

In your functions.php

//capture the post param foo to get the function name 
//set it to null if its not sent
$foo  = isset($_POST['foo']) ? $_POST['foo'] : null;

//if foo is set call the function
if($foo){
$foo();
}

P.S I have no idea why do you want to invoke the function from functions.php, while you can just so it from index.php and include function.php.

Upvotes: 0

Related Questions