jquery - ajax error in wordpress plugin -
i'm writing plugin wordpress. plugin has widget contains button , textbox. button has onclick event. when event occurs need post textbox data php file using ajax ajax code doesn't return error, php doesn't data.
here's js file
function f() { var x=document.getelementbyid('last').value; jquery.ajax({ type: "post", url: 'http://localhost/wordpress/wp-content/plugins/hovo/scripts/vote.php', data: x, success: function() { alert('sends successfully'); }, error: function(jqxhr, textstatus, errorthrown) { alert(errorthrown); } }); }
here's php file
<?php if(isset($_post["anun"])) { echo $_post['anun']; } else { echo "error"; } ?>
please fix problem
in php $_post
expecting key / value pair , sending value in ajax. 'anun' key , whatever stored in x
value.
since not sending key 'anun' $_post
cannot return value stored in x
because if
condition fails.
you should able correct changing ajax call slightly:
function f() { var x = document.getelementbyid('last').value; jquery.ajax({ type: "post", url: 'http://localhost/wordpress/wp-content/plugins/hovo/scripts/vote.php', data: { anun: x }, success: function() { alert('sends successfully'); }, error: function(jqxhr, textstatus, errorthrown) { alert(errorthrown); } }); }
to sure getting correct value of x
, try using console.log(x);
before ajax call , check browser console see returns.
if not return correct value or returns nothing, try changing:
var x = document.getelementbyid('last').value;
to simply:
x = jquery('#last').val();
Comments
Post a Comment