Answer the question
In order to leave comments, you need to log in
Ajax work for form, how to get response?
Here is the function with ajax
function f (e) {
e.preventDefault();
var xhr = new XMLHttpRequest();
var json = JSON.stringify({
name: "login"
});
xhr.open("POST", 'form.php', true)
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.send(json);
xhr.onreadystatechange = function(){
if (xhr.readyState != 4) return;
if (xhr.status != 200){
alert(xhr.status + ': ' + xhr.statusText);
}else{
alert(xhr.responseText)
}
}
}
<?php
if(isset($_POST['login'])){
echo "<br/>login = ". $_POST['login'];
}
?>
Answer the question
In order to leave comments, you need to log in
xhr.open("POST", 'form.php', true)
If the form.php file is in your theme folder, then the address of this file will be site/wp-content/themes/theme-name/form.php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question