Forms help - Confirmation page & sticky checkbox

Here is my code below. What I’m having a problem with is trouble to find how to sticky my checkbox and get to the confirmation page after clicking the submit input. I also need on the confirmation page to list what fields are not filled in.

<head>
	<meta http-equiv="Content-Type"content="text/html; charset=utf-8' />
	<title>Forms</title>
	<style type="text/css" title="text/css" media="all">
	</style>
</head>
<div align="center">
<img src="nba2k20cover.jpg" alt="nba2k20 cover" width="616" height="353" />
<br>
<br>
<?php
include('header.php');
?>
<h4> Hosted by: Zhanos</h4>
<h5> You are required to complete every field to your best!</h5>

<body>

<?php
$name = $_POST['name'];
$comments = $_POST['comments'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$league = $_POST['league'];
$submit = $_POST['submit'];

$monthsarray = array("Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$days = range(1,31);
$daysdefault = array ('Day');
$daysarray = array_merge($daysdefault, $days);
$years = range(2002,1910);
$yearsdefault = array ('Year');
$yearsarray = array_merge($yearsdefault, $years);


?>
<form name ="fbForm" id="fbForm" action="<?php

if(($errors = NULL) && ($_SERVER['REQUEST_METHOD'] == 'POST')) {
	echo "handle.php";
	$submit = true;
} else {
	echo "index.php";
}
?>" method="post">

	<fieldset><legend>Fill out the registration form below:</legend>
	
	<?php
	if(($_POST['name'] == NULL) && ($_SERVER['REQUEST_METHOD'] == 'POST')) {
		echo "<b>Please enter a name!</b>";
	}
	?>
	<p><label>Name: <input type="text" name="name" size="20" maxlength="40" value="<?php echo $_POST['name']; ?>" /></label></p>
	<?php
	if($_POST['email'] == NULL) {
		echo "<b>Please enter your email!</b>";
	}
	?>
	<p><label>Email Address: <input type="text" name="email" size="40" maxlength="60" value="<?php echo $_POST['email']; ?>" /></label></p>
	<?php
	if($_POST['gender'] == NULL) {
		echo "<b>Please select your gender!</b>";
	}
	?>
	<p><label for="gender">Gender:</label><input type="radio" name="gender" value="M"<?php if($_POST['gender'] == "M") { echo "checked"; } ?> /> Male<input type="radio" name="gender" value="F" /> Female</label></p>
	<?php
	if($_POST['month'] == NULL) {
		echo "<b>Please enter a birthday!</b>";
	}	
	?>
	<p><label>Birth Date: 
	<select name="month">
	<?php
	foreach ($monthsarray as $value) {
		if($_POST['month'] == $value) {
			$isselected = "selected";
		} else {
			$isselected = "";
		}
	echo "<option value='$value' $isselected>$value</option> \n";
	}
	?>
	</select>
	<select name="day">
	<?php
	foreach ($daysarray as $value) {
		if($_POST['day'] == $value) {
			$isselected = "selected";
		} else {
			$isselected = "";
		}
	echo "<option value='$value' $isselected>$value</option> \n";
	}
	?>
	</select>
	<select name="year">
	<?php
	foreach ($yearsarray as $value) {
		if($_POST['year'] == $value) {
			$isselected = "selected";
		} else {
			$isselected = "";
		}
	echo "<option value='$value' $isselected>$value</option> \n";
	}
	?>
	</select></label></p>
	<?php
	if($_POST['league'] == NULL) {
		echo "<b>Please select a league!</b>";
	}
	?>
	<p><label for="league">Choose your league:</label><input type="checkbox" name="league" value="A"<?php if($_POST['opt'] == "A") { echo "checked"; } ?> /> A
	<input type="checkbox" name="league" value="B" <?php if($_POST['opt'] == "B") { echo "checked"; } ?>/> B
	<input type="checkbox" name="league" value="C" <?php if($_POST['opt'] == "C") { echo "checked"; } ?>/> C
	<input type="checkbox" name="league" value="D" <?php if($_POST['opt'] == "D") { echo "checked"; } ?>/> D</p>
	<?php
	if($_POST['comments'] == NULL) {
		echo "<b>Write down your questions/concerns if you don't have any write N/A!</b>";
	}
	?>
	<p><label>Questions/Concerns: <textarea name="comments" rows="3" cols="40"><?php echo $_POST['comments']; ?></textarea></label></p>
	
	</fieldset>
	<?php
	if ($submit) {
		echo"<script>document.getElementById('fbForm').submit();</script> ";
	} ?>
	<p align="center"><input type="submit" name="submit" value="REGISTER!" /></p>

</form>
<?php
include('footer.php');
?>


</body>
</html>

OP, do not make duplicate posts.

1 Like

Don’t put your form processing code/validation logic inside the html document. Also, don’t conditionally submit the form to different urls. This is just making more work for you. Keep It Simple (KISS.)

If you are just starting out, you need to get your logic to work for one form field, through whatever you are going to use the submitted data for, then you can worry about the code for the rest of the form fields. By writing out all that logic for every form field, before you even know if what you are doing will work, is just wasting time since you will need to make changes to everything along the way.

Your form processing code should all be together and be above the start of your html document. Your html document/template should be near the end of your file and only contain simple php code needed to display the dynamic parts of the document.

Your form processing code should -

  1. Detect that a post method form has been submitted before referencing any of the submitted form data.
  2. NOT copy variables to other variables without a good reason. A good reason to do so is if you trim all the submitted data. You can do this using a single statement. Not one for each from field.
  3. Validate all the inputs at once, storing validation error messages in an array, using the form field name as the array index. This array is also an error flag. If the array is empty, there are no errors. If the array is not empty, there are errors. You can output the content of this array at the appropriate place(s) in your html document.
  4. If there are no validation errors, use the submitted form data.

Shouldn’t your league check-boxes actually be radio buttons? Can you select more than one league?

For both your gender and league choices, you should have an array of the choices, then dynamically produce the form fields, pre-checking the choice that matches any existing data (when you get to the point of editing data) or the choice that matches the submitted form data. You might want to make sure that the name of your form fields and php form variables match. This is why your existing checkbox code doesn’t pre-check anything.

Any dynamic values that get echoed on a web page should have htmlentities() applied to them to help prevent cross site scripting.

It also appears that you don’t have php’s error_reporting set to E_ALL and display_errors set to ON as the existing code should be producing a lot of errors. You should put these settings in the php.ini on your system.

Sponsor our Newsletter | Privacy Policy | Terms of Service