Skip to content

Instantly share code, notes, and snippets.

@nicholaskajoh
Created September 9, 2017 16:03
Show Gist options
  • Save nicholaskajoh/015857712c612946105542eb8f99d4dd to your computer and use it in GitHub Desktop.
Save nicholaskajoh/015857712c612946105542eb8f99d4dd to your computer and use it in GitHub Desktop.
Form submission -- CDC
<?php
$fields = [];
$fields['name'] = $_POST['name'] ?? NULL;
$fields['sex'] = $_POST['sex'] ?? NULL;
$fields['q1'] = $_POST['q1'] ?? NULL;
$fields['q2'] = $_POST['q2'] ?? NULL;
$fields['q3'] = $_POST['q3'] ?? NULL;
// validation
$errors = [];
foreach($fields as $k => $v) {
if(is_null($v) || $v == "") $errors[] = "The {$k} field is required!";
}
if(empty($errors)) {
echo '<h1 style="color: green;">Thanks you very plenty!</h1>';
print_r($fields);
} else {
echo '<h1 style="color: red;">Errors man! Errors...</h1>';
print_r($errors);
}
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body
{
font-family: "Montserrat", sans-serif
}
h1
{
color: green;
}
#wrapper
{
position: absolute;
left: 40%;
width: 500px;
}
.field{
display: block;
padding: 10px;
}
.long-label
{
display: block;
}
input[type="text"]
{
width: 300px;
}
textarea
{
width: 300px;
}
</style>
<script type="text/javascript">
function notify () {
alert("Thanks for Sharing!");
}
</script>
</head>
<body>
<div id="wrapper">
<h1>How Was Your Day?</h1>
<form action="form_submit.php" method="POST">
<div class="field">
<label class="long-label">Name</label>
<input type="text" name="name"></div>
<div class="field">
<label class="long-label">Sex </label>
<label>Male</label><input type="radio" value="Male" name="sex">
<label>Female</label><input type="radio" value="Female" name="sex"></div>
<div class="field">
<label class="long-label">Did you wake up happy?</label>
<label>Yes</label>
<input type="radio" value="Yes" name="q1">
<label>No</label><input type="radio" value="No" name="q1"></div>
<div class="field">
<label class="long-label">How many classes did you have today?</label>
<input type="text" name="q2"></div>
<div class="field">
<label class="long-label">What was the best thing that happened to you today?</label>
<textarea id="experience" name="q3"></textarea></div>
<div class="field">
<input type="submit" onclick="notify()"></div>
<!-- do onsubmit -->
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment