Hi everyone,
I am a junior developer for almost 4 month in London, and I am trying to create a registration form with html and php.
Everything is working well, but I want to restrict the user to resubmit the form again if clicks back button on browser to avoid duplicate results , or to insert black field if they access the register.php script which I use to save the data from registration.php.
Any help will be appreciate.
I have the following code for registration.php:
<?php // RAY_multi_submit.php
error_reporting(E_ALL);
// ALWAYS START THE PHP SESSION ON EVERY PAGE
session_start();
?>
<!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, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Small Foot Registration Page</title>
<!-- Bootstrap core CSS -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="assets/css/form-validation.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length >= 3){
$.get("lookup.php", {term: inputVal}).done(function(data){
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result option", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
<style>
#result{
width: 100%
}
.search-box select, .search-box select:focus{
border-radius: 6px;
height: 40px;
margin:0 auto;
margin-top: 15px;
font-size:15px;
padding:10px 10px 10px 5px;
display:block;
border:1px solid #C0C0C0;
}
</style>
</head>
<body class="bg-light">
<div class="container">
<div class="py-0 text-center pt-0 pb-0 ">
<img src="assets/img/reg_banner.jpg" width="auto" height="auto" class="h-auto w-auto mw-100 mh-100 position-static mt-0 pb-5">
</div>
<div class="row">
<div class="order-md-1 col-md-12">
<h4 class="mb-3">Registration details</h4>
<form id="registration" class="needs-validation" action="newreg.php" method="post" >
<div class="row">
<div class="col-md-12 mb-3 search-box">
<label for="school">School Name</label>
<input type="text" class="form-control" id="school" value="" name="school" placeholder="Enter name or postcode" required>
<select name="school_id" id="result" class="result" placeholder="Enter name or postcode" required>
<option value="" placeholder="Enter name or postcode">Your Search Result it will apear here</option>
</select>
<div class="invalid-feedback">
Please enter your school name.
</div>
<div class="help-block with-errors"></div>
</div>
<!-- <div class="col-md-12 mb-3">
<label for="otherSchool">Insert here your school in case it is not in our list</label>
<input type="text" class="form-control" id="otherSchool" value="" name="otherSchool" placeholder="" required>
<div class="invalid-feedback">
Valid first name is required.
</div>
<div class="help-block with-errors"></div>
</div> -->
<div class="col-md-6 mb-3">
<label for="firstName">First name</label>
<input type="text" class="form-control" id="firstName" value="" name="firstName" placeholder="" required>
<div class="invalid-feedback">
Valid first name is required.
</div>
<div class="help-block with-errors"></div>
</div>
<div class="col-md-6 mb-3">
<label for="lastName">Last name</label>
<input type="text" class="form-control" id="lastName" value="" name="lastName" placeholder="" required>
<div class="invalid-feedback">
Valid first name is required.
</div>
<div class="help-block with-errors"></div>
</div>
<div class="col-md-6 mb-3">
<label for="email">Email</label>
<div class="invalid-feedback">
Valid first name is required.
</div>
<div class="help-block with-errors"></div>
<input type="email" name="email" class="form-control" id="email" value="" placeholder="you@example.com">
</div>
<div class="col-md-6 mb-3">
<label for="subject">Subject Taught</label>
<input type="text" class="form-control" name="subject" id="subject" value="" placeholder="" required>
<div class="invalid-feedback">
Valid last name is required.
</div>
<div class="help-block with-errors"></div>
</div>
<div class="col-md-6 mb-3">
<label for="phone">Telephone number</label>
<div class="invalid-feedback">
Valid first name is required.
</div>
<div class="help-block with-errors"></div>
<input type="text" name="phone" class="form-control" id="phone" value="" placeholder="">
</div>
<div class="col-md-6 mb-3">
<label for="jrole">Job Role</label>
<div class="invalid-feedback">
Valid first name is required.
</div>
<div class="help-block with-errors"></div>
<select class="custom-select d-block w-100" name="jrole" id="jrole" required>
<option value="">Choose...</option>
<option value="Teacher">Teacher</option>
<option value="Teaching Assistant / Learning Support Assistant">Teaching Assistant / Learning Support Assistant</option>
<option value="SEN Teacher">SEN Teacher</option>
<option value="Senior Management Team">Senior Management Team</option>
<option value="PRU / Alternative Provision Teacher">PRU / Alternative Provision Teacher</option>
<option value="Youth Leader">Youth Leader</option>
<option value="Librarian / Learning resources manager">Librarian / Learning resources manager</option>
<option value="Parent / Carer">Parent / Carer</option>
<option value="Home / Educator">Home / Educator</option>
<option value="Other">Other</option>
</select>
</div>
</div>
<div class="mb-3">
<label for="jtitle">Job Title</label>
<input type="text" class="form-control" name="jtitle" id="jtitle" value="" placeholder="">
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="subjects_and_specialisms">Subjects & Specialisms</label>
<select multiple name="subjects_and_specialisms[]" class="form-control" id="subjects_and_specialisms">
<option value="Art and Design">Art and Design</option>
<option value="Citizenship">Citizenship</option>
<option value="Economics and Business">Economics and Business</option>
<option value="Film Studies">Film Studies</option>
<option value="Gaelic Scottish (Gàidhlig)">Gaelic Scottish (Gàidhlig)</option>
<option value="History">History</option>
<option value="Languages">Languages</option>
<option value="Literacy">Literacy</option>
<option value="Media Studies">Media Studies</option>
<option value="Music">Music</option>
<option value="Performing Arts">Performing Arts</option>
<option value="Politics">Politics</option>
<option value="Psychology">Psychology</option>
<option value="Science">Science</option>
<option value="Welsh First Language (Cymru)">Welsh First Language (Cymru)</option>
<option value="Careers">Careers</option>
<option value="Design and Technology">Design and Technology</option>
<option value="English">English</option>
<option value="Gaelic Irish (Gaeilge)">Gaelic Irish (Gaeilge)</option>
<option value="Geography">Geography</option>
<option value="Information Computing Technology">Information Computing Technology</option>
<option value="Law">Law</option>
<option value="Mathematics">Mathematics</option>
<option value="Moving Image Arts">Moving Image Arts</option>
<option value="Numeracy">Numeracy</option>
<option value="Physical Education">Physical Education</option>
<option value="PSHE Education">PSHE Education</option>
<option value="Religious Education">Religious Education</option>
<option value="Special Educational Needs">Special Educational Needs</option>
<option value="Welsh Second Language (Cymru)">Welsh Second Language (Cymru)</option>
</select>
</div>
</div>
<div class="row">
</div>
<hr class="mb-4">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="futurefilm" value="1" id="futurefilm">
<label class="custom-control-label" for="futurefilm">We work with many of the leading film studios and entertainments brands to create amazing resources to help inspire children through education. Please tick this box if you would like to know more about future film resources</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="t_eval" value="1" id="t_eval">
<label class="custom-control-label" for="t_eval">As part of SUPER.s commitment to creating outstanding educational resources, we would love to hear your thoughts and opinions on how we can continue to improve our resources. Tick here if you would like to take part in our teacher resource evaluation.</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="p_eval" value="1" id="p_eval">
<label class="custom-control-label" for="p_eval">Tick here if you would like to take part in our paid for parent resource evaluation.</label>
</div>
<hr class="mb-4">
<button class="btn btn-primary btn-lg btn-block" type="submit">Register</button>
</form>
</div>
</div>
<footer class="my-5 pt-5 text-muted text-center text-small">
<p class="mb-1">© 2018 SUPER.</p>
<ul class="list-inline">
<li class="list-inline-item">
<a href="#">Privacy</a>
</li>
<li class="list-inline-item">
<a href="#">Terms</a>
</li>
<li class="list-inline-item">
<a href="#">Support</a>
</li>
</ul>
</footer>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/popper.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
<?php
// A FUNCTION TO RETURN TRUE OR FALSE ABOUT MULTI-SUBMIT CONDITIONS
function multi_submit($type="POST")
{
// MAKE THE FUNCTION WORK FOR EITHER GET OR POST SUBMITS
$input_array = (strtoupper($type) == "GET") ? $_GET : $_POST;
// GATHER THE CONTENTS OF ALL THE SUBMITTED FIELDS AND MAKE A MESSAGE DIGEST
$string = NULL;
foreach ($input_array as $val)
{
// CONCATENATE ALL SUBMITTED VALUES
$string .= $val;
}
$string = md5($string);
// IF THE SESSION VARIABLE IS NOT SET THIS IS NOT A MULTI-SUBMIT
if (!isset($_SESSION["_multi_submit"]))
{
// SAVE THE SUBMITTED DATA MESSAGE DIGEST
$_SESSION['_multi_submit'] = $string;
return FALSE;
}
// IF THE SESSION DATA MATCHES THE MESSAGE DIGEST THIS IS A MULTI-SUBMIT
if ($_SESSION['_multi_submit'] === $string)
{
return TRUE;
}
else
{
// SAVE THE MESSAGE DIGEST TO DETECT FUTURE MULTI-SUBMIT
$_SESSION['_multi_submit'] = $string;
return FALSE;
}
}
// SHOW HOW THIS IS DONE
if (!empty($_POST))
{
if (multi_submit())
{
die("ALREADY GOT THAT");
}
}
?>
And this is the script that is taking and saving the data to database:
<?php
include('vendor/config.php');
session_start();
// $link = mysqli_connect($servername, $username, $password, $dbname);
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
if (isset($_POST)) {
$school = $mysqli->real_escape_string($_REQUEST["school_id"]);
$firstname = $mysqli->real_escape_string($_REQUEST['firstName']);
$lastname = $mysqli->real_escape_string($_REQUEST['lastName']);
$email = $mysqli->real_escape_string($_REQUEST['email']);
$subject_taught = $mysqli->real_escape_string($_REQUEST['subject']);
$telephone = $mysqli->real_escape_string($_REQUEST['phone']);
$job_role = $mysqli->real_escape_string($_REQUEST['jrole']);
$job_title = $mysqli->real_escape_string($_REQUEST['jtitle']);
$subjects_and_specialisms = $_POST['subjects_and_specialisms'];
for($i = 0; $i < count($subjects_and_specialisms); $i++){
$subjects = $subjects.','.$subjects_and_specialisms[$i];
}
$futurefilm = $mysqli->real_escape_string($_REQUEST['futurefilm']);
$t_eval = $mysqli->real_escape_string($_REQUEST['t_eval']);
$p_eval = $mysqli->real_escape_string($_REQUEST['p_eval']);
if ($t_eval == '') { $t_eval = 0; }
if ($p_eval == '') { $p_eval = 0; }
if ($futurefilm == '') { $futurefilm = 0; }
$sql = "INSERT INTO newregistrations (school, firstName, lastName, email, subject, phone, jrole, jtitle, subjects_and_specialisms, futurefilm, t_eval, p_eval, createdAt)
VALUES ('$school', '$firstname', '$lastname', '$email', '$subject_taught', '$telephone', '$job_role', '$job_title', '$subjects', '$futurefilm', '$t_eval', '$p_eval', NOW())";
if($mysqli->query($sql) === true){
header('Location: thankyou.php');
exit();
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
$mysqli->close();
}
?>