My code now is in separate conditions. If I submit my form without insert any data, it must be two warning come out; if I insert no.of people, and submit it will show the required of age; if I insert the age and submit it will show the required of people.
But now when i submit my code without anything, it only show the warning of age? How to fix it by only php so that when i submit the form without anythings and two warning will pop out and call must be fill in this two fields
This code is process.php
<?php
$errors = [];
if (empty($_POST['people'])) {
$errors[] = 'people';
}
if (empty($_POST['age'])) {
$errors[] = 'age';
}
if (count($errors)) {
header('Location: form.php?error=' . implode(",", $errors));
}
$person = $_POST['people'];
$ages = $_POST['age'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$date = date('H:i, jS F Y');
?>
<html>
<head>
<title>Result of Carbon Footprint Calculator </title>
</head>
<body>
<?php
echo "<p>Calculation processed at ".date('H:i, jS F Y')."</p>";
echo "<p>The calculation is as follow: </p>";
if ($person > 0) {
echo $person." person in household<br/>";
}
if ($ages > 0) {
echo $ages." age<br/>";
}
$outputstring = $date."\t".$person." \t".$ages." \t";
@ $fp = fopen("$DOCUMENT_ROOT/car/ttl.txt", 'ab');
flock($fp, LOCK_EX);
if (!$fp) {
echo "<p><strong> Your order could not be processed at this time.
Please try again later.</strong></p></body></html>";
exit;
}
// write the file
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
echo "<p>Calculation complete.</p>";
?>
</body>
</html>
This code is form.php
<?php
$errors = isset($_REQUEST['errors']) ? $_REQUEST['errors'] : '';
if ($errors) {
$arr = explode(",", $errors);
foreach ($arr as $error) {
if ($error == 'people') {
echo "you need to enter name<br>";
}
if ($error == 'age') {
echo "you need to enter age<br>";
}
}
}
?>
<html>
<head>
<h1>Members</h1>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<form method="post" action="process.php">
<tr>
<td >Number of Person</td>
<td><input type="text" name="people" >
<span class="error"></span>
</td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" size="18" maxlength="18">
<span class="error"></span>
</td>
</tr>
<tr>
<th colspan="2" align="right"><input type="submit" name="submit" value="submit"></th>
</tr>
</table>
</form>