-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax4.html
75 lines (65 loc) · 2.04 KB
/
ajax4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!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>
<button id="button">Get Name</button>
<hr />
<h1>GET FORM</h1>
<form method="GET" action="process.php">
<input type="text" name="name" /> <input type="submit" value="Submit" />
</form>
<br />
<h1>GET FORM AJAX</h1>
<form id="getForm">
<input type="text" name="name" id="name1" />
<input type="submit" value="Submit" />
</form>
<br />
<h1>POST FORM</h1>
<form method="POST" action="process.php">
<input type="text" name="name" id="name1" />
<input type="submit" value="Submit" />
</form>
<br />
<h1>AJAX POST FORM</h1>
<form id="postForm">
<input type="text" name="name" id="name2" />
<input type="submit" value="Submit" />
</form>
<script>
document.getElementById("button").addEventListener("click", getName);
document.getElementById("getForm").addEventListener("submit", getName);
document.getElementById("postForm").addEventListener("submit", postName);
function getName(e) {
e.preventDefault();
var name = document.getElementById("name1").value;
var xhr = new XMLHttpRequest();
xhr.open("GET", "process.php?name=" + name, true);
xhr.onload = function() {
console.log(this.responseText);
};
xhr.send();
}
function postName(e) {
e.preventDefault();
var name = document.getElementById('name2').value;
var params = "name="+name;
var xhr = new XMLHttpRequest();
xhr.open("POST", "process.php", true);
xhr.setRequestHeader(
"Content-type",
"application/x-www-form-urlencoded"
);
xhr.onload = function() {
console.log(this.responseText)
};
xhr.send(params);
}
</script>
</body>
</html>