1

Is it possible to add php to a jquery function? On ajax success I'm displaying some text in a div using the .text() function.

success: function(html) {
  msg_each.nextAll('.msg_box:first').text('show some text...').fadeIn(200);
}

Via jquery, I would also like to use a Wordpress function:

<?php comments_popup_link('Post Comment', '1 Comment', '% Comments'); ?> 

How can I add that to the .text() function?

0

1 Answer 1

3

PHP runs on the server. JavaScript runs in the browser. You need to use Ajax (something like $.get() to perform an HTTP get on a resource on your server. This will return (probably) HTML or plain text that you can pass into your .text() call.

Basic skellington

success: function (html) {
    $.get('url/to/page.php', function (data) {
        msg_each.nextAll('.msg_box:first').text(data).fadeIn(200);  
    });              
}

Since you're already making an Ajax call, however, you might just be able to modify the server-side page you're querying to include the additional HTML (generated by the comments_popup_link() call) so you don't have to make an additional XHR.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the code! for function(data) do I have to put the name of the function used for comments_popip_links in 'data' ?
@Noob: I don't get what you're asking. Could you clarify? PHP and JavaScript are totally separate.
Sorry, I misunderstood. You totally make sense. I meant to ask, can you output the result of a php function in a js function? For example if a function named test() echoes hello world can I use that in js? Perhaps pass it in a variable

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.