Hi,
So I have a problem with the code. It should write to the data.csv file, but it does not create it nor write to it if I create it manually. Would be happy if someone could help me ASAP as I need to defend this lab in a few hours
TL;DR rights for the file are 0655
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="author" content="Nikita">
<title>LAB 5 - PHP Form</title>
</head>
<body>
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="download.php">Download Data</a></li>
</ul>
</nav>
<h1>Registration Form</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="nameFirst">First Name:</label>
<input type="text" id="nameFirst" name="nameFirst" required><br><br>
<label for="nameMiddle">Middle Name:</label>
<input type="text" id="nameMiddle" name="nameMiddle"><br><br>
<label for="nameLast">Last Name:</label>
<input type="text" id="nameLast" name="nameLast" required><br><br>
<label for="salutation">Salutation:</label>
<select id="salutation" name="salutation" required>
<option value="Mr.">Mr.</option>
<option value="Ms.">Ms.</option>
<option value="Mrs.">Mrs.</option>
<option value="Sir">Sir</option>
<option value="Prof.">Prof.</option>
<option value="Dr.">Dr.</option>
</select><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required min="18" max="98"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="phone">Phone(Including country code):</label>
<input type="tel" id="phone" name="phone" required><br><br>
<label for="dateArrive">Date of Arrival:</label>
<input type="date" id="dateArrive" name="dateArrive" required><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Define an array to store validation errors
$errors = array();
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Validate first name
if (isset($_POST['nameFirst']) && !preg_match('/^[a-zA-Z]+$/i', $_POST['nameFirst'])) {
$errors[] = 'First name must not contain digits or special characters';
}
// Validate middle name
if (isset($_POST['nameMiddle']) && !preg_match('/^[a-zA-Z]+$/i', $_POST['nameMiddle'])) {
$errors[] = 'Middle name must not contain digits or special characters';
}
// Validate last name
if (isset($_POST['nameLast']) && !preg_match('/^[a-zA-Z]+$/i', $_POST['nameLast'])) {
$errors[] = 'Last name must not contain digits or special characters';
}
// Validate salutation
$allowedSalutations = array('Mr.', 'Ms.', 'Mrs.', 'Sir', 'Prof.', 'Dr.');
if (isset($_POST['salutation']) && !in_array($_POST['salutation'], $allowedSalutations)) {
$errors[] = 'Invalid salutation';
}
// Validate age
if (isset($_POST['age']) && ($_POST['age'] < 18 || $_POST['age'] > 98)) {
$errors[] = 'Age must be between 18 and 98';
}
// Validate email
if (isset($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email address';
}
// Validate phone number
if (isset($_POST['phone']) && !preg_match('/^(\+?\d{1,3}[\s-]?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/', $_POST['phone'])) {
$errors[] = 'Invalid phone number';
}
// Validate arrival date
if (isset($_POST['dateArrive'])) {
$minDate = new DateTime('2023-01-01');
$maxDate = new DateTime('2033-01-01');
$arrivalDate = DateTime::createFromFormat('Y-m-d', $_POST['dateArrive']);
if (!$arrivalDate || $arrivalDate < $minDate || $arrivalDate > $maxDate) {
$errors[] = 'Invalid arrival date';
}
}
// If there are no validation errors, store the form data
if (empty($errors)) {
// Define the name of the CSV file
$filename = 'data.csv';
// Define the data to be written to the CSV file
$data = array(
$_POST['nameFirst'],
$_POST['nameMiddle'],
$_POST['nameLast'],
$_POST['salutation'],
$_POST['age'],
$_POST['email'],
$_POST['phone'],
$_POST['dateArrive'],
);
// If the file exists, add the data to it or if no file, create it
if (file_exists($filename)) {
$file = fopen($filename, 'a');
} else {
$file = fopen($filename, 'w');
// Write header row
fwrite($file, array('First Name', 'Middle Name', 'Last Name', 'Salutation', 'Age', 'Email', 'Phone', 'Date of Arrival'));
}
// Write the data to the CSV file
fwrite($file, $data);
// Close the file handle
fclose($file);
// Display a success message
echo '<p><div id="confirmedText">Thank you for registering!</p>';
echo '<p>Registration details:</p>';
echo '<ul>';
echo '<li>Name: '.$_POST['salutation'].' '.$_POST['nameFirst'].' '.$_POST['nameMiddle'].' '.$_POST['nameLast'].'</li>';
echo '<li>Age: '.$_POST['age'].'</li>';
echo '<li>Email: '.$_POST['email'].'</li>';
echo '<li>Phone: '.$_POST['phone'].'</li>';
echo '<li>Date of Arrival: '.$_POST['dateArrive'].'</li>';
echo '</ul>';
} else {
// If there are validation errors, display them
echo '<ul>';
foreach ($errors as $error) {
echo '<div id="confirmedError">' . implode('<br>', $errors) . '</div>';;
}
echo '</ul>';
} }
?>
</body>
</html>