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?
1 Answer 1
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.
3 Comments
function(data) do I have to put the name of the function used for comments_popip_links in 'data' ?test() echoes hello world can I use that in js? Perhaps pass it in a variable