0

Basically what I am trying to do is pass a callback function through PHP using JQuery $.post. Something like:

function loadPage()
{
    $.post('myurl.php', { 'callbackfunc' : function (info) { alert(info); } }, 
        function(data) {
            $("#divid").html(data);
     } );
}

Where myurl.php creates a page with a table. The table rows have an onclick handler that calls the callback function.

The problem is, passing the function this way sends it as undefined via post, and if I enclose it in quotes, then when I try to call it I get the error that it is not a function.

Is there any way to do what I am describing?

2
  • 1
    may i ask what you plan to do with this function in php? Commented Oct 28, 2011 at 19:12
  • The AJAX post creates a table with information from the database in it. When the user clicks on a row in the table, the callback function gets called. This allows the table to be portable and have the onclick be different every where that it is used. Commented Oct 28, 2011 at 19:13

4 Answers 4

1

there is a way but not normally recommended. You can pass the function as a string and then when you receive the function with javascript use the eval() function.

function loadPage()
{
    $.post('myurl.php', { 'callbackfunc' : "function (info) { alert(info); }" }, 
        function(data) {
            $("#divid").html(eval(data));
     } );
}
Sign up to request clarification or add additional context in comments.

Comments

0

pass it as a string (function body in double quotes, probably using escape characters).

2 Comments

function is a normal javascript variable. It is not called here.
still the following needs to be done: { 'callbackfunc' : "function (info) { alert(info); }" }
0

You could attach your function to your table rows after the table has been returned, rather than try to have the php code create it with the proper callback.

function loadPage(callbackfunc)
{
    $.post('myurl.php', {}, 
        function(data) {
            $("#divid").html(data);
            $("#divid").find("tr").each($(this).click(callbackfunc));
    } );
}

Comments

0

there is no way to do what you are describing, but there is a probably way to do what you want. ( i dont know what you want )

you cant pass a JS function to the server. you can only pass strings.

edit: based on your response, i can tell you that your way of doing this is not only not good, but dangerous, as it allows XSS.

it would be better to delegate the click function to the div where you are inserting the table like this:

function loadPage(){
    $("#divid").delegate('tr', 'click', function(evt){
      //this code is different per page, change color, alert, etc
    });
    //after the above event is delegated, you can reload the table as much as you want, without needing to rebind the above event
    $.post('myurl.php', function(data) {
        $("#divid").html(data);
    });
}

1 Comment

The best way I can think of how to explain it is, I have three pages that all need the same table, so all three pages call the same $.post to AJAXically load the table. Each page, however, does something different when the user clicks on a row in the table. For page 1, I want to alert the row id, for page 2, I want to change the color, and for page 3, I want to do a redirect. Thus I want to pass a different callback function from each page that will be called through the tr's onclick handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.