My aim is to create the most simple ajax call in WordPress, but I can’t get my head around how it works.
JS File:
jQuery("#votepostform").submit(function() {
var url = "file.php"; // php script to handle form
jQuery.ajax({
type: "POST",
url: url,
data: jQuery("#votepostform").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
return false; // avoid to execute the actual submit of the form.
});
Form HTML:
<form action="" method="post" id="votepostform" />
<input type="hidden" name="postid" value="333" />
<button type="send" name="vote" class="vote_link"></button>
</form>
file.php:
echo '<script language=\'javascript\'>alert(\'It works! \');</script>';
How can I make this ajax code work in a WordPress site?