How do i call a php function inside the Jquery Code
I have a php function which get the Details when passed an number .So, i have a dynamic table where each row contains insert button which performs some action on the row record .Now Here i have to call php function getdetails and pass the data-num开发者_开发技巧ber and get the details from the function and create an array insert in to session
$(".bttn_insert").live('click',function(){
var data-number = $(this).attr('data-number');
//Now Here i have to call php function getdetails and pass the data-number and get the details from the function
and create an array insert in to session
<?php getdetails('data-number'),?>
});
Javascript is executed on the client, and PHP is executed on the server, therefore you need to make a call to the server, for example though AJAX.
You should use AJAX e.g. $.ajax, $.get, $.post
$.ajax({
url: 'ajax/test.php',
success: function(data) {
$('.result').html(data);
alert('Load was performed.');
}
});
In your PHP file just echo the result you want to receive.
Read more: [LINK]
Ajax as descibe in other post, but this is what code you would need to do:
$(".bttn_insert").live('click',
function()
{
var post_data = {
data_number:$(this).attr('data-number')
}
$.post('getdetails.php',post_data,function(data){
//Gets called upon completion.
$('#some_selector p').html(data);
})
}
);
and the PHP file, depending on the way your configuration is:
<?php
//include 'your functions';
//or
/*
function getDetails(){...}
*/
if(!empty($_POST['data_number']))
{
if(is_numeric($_POST['data_number']))
{
echo getDetails( (int)$_POST['data_number'] );
}
}
?>
there is no way to use a php function directly from jquery, you have to create a php script to encapsulate your function call then call your php script via jquery ajax functions.
If you want to insert php calls to generate javascript code this is a different issue as I don't think thatgenerating directly into your jquery code would be a good idea, you should probably generate a separated jsonp output from your php then use the object in jquery.
精彩评论