I do not know why this response me -1 always? I am using this code in my plugin file.
i want response on click p tag
<?php function myajax(){ ?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#testbutton").click(function(){
jQuery.ajax({
url: "<?php echo admin_url('admin-ajax.php'); ?>",
type: 'POST',
data: {
action: 'stravyfuncajax22'
},
dataType: 'html',
success: function(response) {
alert(response);
}
});
});
});
</script><?php
echo'
<p id="testbutton1">click</p>';
}
add_shortcode("my_ajax", "myajax");
add_action('wp_ajax_stravyfuncajax22', 'testfunc11');
add_action('wp_ajax_nopriv_stravyfuncajax22', 'testfunc11');
function testfunc11() {
echo "2";
die();
}
?>
EDIT : added php tag now….
according to wordPress jedi, scribu, you can now enqueue scripts from inside the plugin handler.
http://scribu.net/wordpress/conditional-script-loading-revisited.html
you were echoing things and in shortcode handling you have to return. i ran into some issues getting the script to work by returning the script, so i moved it to an external file which definitely works.
and then the my-script.js
hope that helps.
EDIT 1
i didn’t test this part ,but i think it should work for echoing a form value as the ajax response.
`
function myajax(){
`
new javascript
Try JSON encoding your response before echoing it like so (don’t forget the header either!).
And then up in your JS code..
And it should show you ‘2’.
However, I wouldn’t recommend using
jQuery.ajax
, instead I would recommend using this method which is (in my opinion) much easier to read and use.