views:

53

answers:

5

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-number 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'),?>

  });
+4  A: 

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]

infinity
Instead of echoing plain text, it's often better to use JSON which support string/numeric/boolean/array/object types. Just use `echo json_encode($data);` and add the parameter `dataType: 'json'` to the `$.ajax` call.
Znarkus
Yes you are right, JSON should be used, so you can all kind of data as stated above.
infinity
A: 

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.

dvhh
This is in case you want to output JavaScript through PHP file.
infinity
note taken and edited my comment to reflect it
dvhh
+4  A: 

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.

Douglas Leeder
A: 

Use Ajax or JSON :-)

Enrico Pallazzo
*or* JSON? :-))
Znarkus
Hey Json, come over here. I need some help :D
infinity
http://api.jquery.com/jQuery.post/
Enrico Pallazzo
+1  A: 

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'] );
    }
}
?>
RobertPitt