Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php 
	session_start();
?>
<html>
<head>
		<title>Form Validation</title>
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous"></link>
	</head>
	
	<body>
	
	<div class="container">
	<h2>Form Validation</h2>
	
	<!-- Disply required fields -->
	<?php if (array_key_exists('missingFields', $_SESSION) && count ($_SESSION['missingFields']) >0) { ?>
		<div class="alert alert-danger">
		<p> The following field(s) are required:</p>
		<ul>
			<?php
				foreach($_SESSION['missingFields']as $field){?>
					<li><?php echo $field;?></li>
					
				<?php }?>
		
		</ul>
		</div>
	
	<?php $_SESSION['missingFields']=[];
		$name=$_SESSION['name'];
		$age=$_SESSION['age'];
		$username=$_SESSION['username'];
	}?>
	 
	 
	 <!-- Disply error messages-->
	<?php if (array_key_exists('errorMessage', $_SESSION) && count ($_SESSION['errorMessage']) >0) { ?>
		<div class="alert alert-danger">
		<p> Form errors:</p>
		<ul>
			<?php
				foreach($_SESSION['errorMessage']as $message){?>
					<li><?php echo $message;?></li>
				<?php }?>
		
		</ul>
		</div>
	
	<?php $_SESSION['errorMessage']=[];
		$name=$_SESSION['name'];
		$age=$_SESSION['age'];
		$username=$_SESSION['username'];
	} ?>
	 
	 
	 
	
		<form action="script/script.php"method="post">
			<div class="form-group">
				<label for="name">Name:</label>
				<input type="text" name="name" id="name" class="form-control" value="<?php
				echo($name);?>">
			</div>
			<div class="form-group">
					<label for="age">Age:</label>
					<input type="text" name="age" id"age" class="form-control" value="<?php
				echo($age);?>">
				</div>
				<div class="form-group">
				<label for="gender">Gender:</label><br>
					<label>
						<input name="gender" type="radio" value="m" >Male</label>
					<label>
						<input name="gender" type="radio" value="f" >Female</label>
					<label>
						<input name="gender" type="radio" value="o" >Other</label>
				</div>	
				
				<div class="form-group">
				<label for="name">User Name:</label>
				<input type="text" name="username" id="username" class="form-control" value="<?php echo($username);?>">
			</div>
			
			<div class="form-group">
				<label for="name">Password:</label>
				<input type="password" name="password" id="password" class="form-control">
			</div>
				
			<div class="form-group">
				<input name="submit" type="submit" class="btn btn-primary" value="Submit Form">
				</div>
		</form>
	</div>
	</body>
	</html>


What I have tried:

I've tried many times to read through it, and I've caught a couple of syntax errors. please i'm new to coding and i need it asap.
thank you
Posted
Comments
Patrice T 6-Jan-21 20:30pm    
describe your problems in detail
list exact error messages.
thatraja 6-Jan-21 23:31pm    
Include error messages in your question.
W Balboos, GHB 7-Jan-21 11:08am    
Aside from agreeing with those first two comments, I have found that when I start a session it is best put on the same line as <?php

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900