submit data to the server with a post request through Ajax, and output the value of the $_ post superglobal variable in PHP. The output is shown in the following figure
:
//demo3.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="Demo3.php" method="POST">
:<input type="text" name="username" id="un"><br/>
:<input type="text" name="password" id="pw"><br/>
<input type="submit" id="submit" value="">
</form>
<p id="tips">
</body>
</html>
<script>
var submit_btn = document.getElementById("submit");
var xhr = new XMLHttpRequest();
submit_btn.onclick = function(e){
e.preventDefault();
xhr.onreadystatechange = function(){
if(xhr.readyState==4 && xhr.status==200){
alert(xhr.responseText);
}
}
var username = document.getElementById("un").value; //
var data = "username="+username;
xhr.open("post","Demo3.php");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencode");
xhr.send(data);
}
</script>
//Demo3.php
<?php
echo ($_POST);
is there a god who knows what the problem is?