-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
87 lines (80 loc) · 3.71 KB
/
index.php
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
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<title>Mailing Address</title>
</head>
<body>
<div class="container d-flex justify-content-center">
<form class="col-md-6 m-3" id="address-form">
<div class="form-group">
<label for="street">Street Address</label>
<input type="text" class="form-control" id="street" name="street">
</div>
<div class="form-group">
<label for="city">City</label>
<input type="text" class="form-control" id="city" name="city">
</div>
<div class="form-group">
<label for="state">State</label>
<select class="form-control" id="state" name="state">
<option value=""></option>
<?php
require('states.php');
foreach($states as $key => $value) {
echo "<option value='$key'>$value</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="zip">Zip Code</label>
<input type="text" class="form-control" id="zip" name="zip">
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="true" id="standardized" name="standardized">
<label class="form-check-label" for="standardized">
Standardized Address
</label>
</div>
<button type="submit" class="btn btn-primary" id="submitBtn">Submit</button>
<div class="alert alert-danger my-3" role="alert" id="error" style="display:none;"></div>
<div class="alert alert-success my-3" role="alert" id="success" style="display:none;"></div>
</form>
</div>
<script>
// Get the form element
const form = document.getElementById("address-form");
const errorAlertEl = document.getElementById("error");
const successAlertEl = document.getElementById("success");
const submitBtn = document.getElementById('submitBtn');
// Listen for the form submit event
form.addEventListener("submit", function(event) {
event.preventDefault();
const xhr = new XMLHttpRequest();
xhr.open("POST", "submit.php", true);
const formData = new FormData(form);
xhr.send(formData);
submitBtn.disabled = true;
xhr.onload = () => {
submitBtn.disabled = false;
const { message } = JSON.parse(xhr.responseText)
if (xhr.readyState === 4 && xhr.status === 200) {
showHideAlert(successAlertEl, errorAlertEl, message)
} else {
showHideAlert(errorAlertEl, successAlertEl, message)
}
};
});
function showHideAlert(showEl, hideEl, msg){
showEl.style.display = 'block';
showEl.innerHTML = msg;
hideEl.style.display = 'none';
}
</script>
</body>
</html>