我正在尝试创建一个表单,其中有人输入姓名(非必填),电子邮件地址和学校科目列表(非必填)。由于某些原因,如果满足所有检查要求(输入的名称为全字母或空白,表示电子邮件有效且完整),则提交的内容仍未添加到我的CSV文件中。
代码如下:
<?php
$error = '';
$name = '';
$email = '';
$subjects = '';
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST["submit"])) {
$name = clean_text($_POST("user_name"));
if(!preg_match("/^[a-zA-Z ]*$/",$name)) {
$error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
}
if(empty($_POST["user_mail"])) {
$error .= '<p><label class="text-danger">Please Enter your Email</label></p>';
} else {
$email = clean_text($_POST["user_mail"]);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error .= '<p><label class="text-danger">Invalid email format</label></p>';
}
}
$subjects = clean_text($_POST["user_subjects"]);
if($error == ''){
$file_open = fopen("data.csv", a);
$num_rows = count(file("data.csv"));
if($num_rows > 1) {
$num_rows = ($num_rows - 1) + 1; //Generates serial number
}
$form_data = array(
'serial_num' => $num_rows,
'name' => $name,
'email' => $email,
'subjects' => $subjects
);
fputcsv($file_open, $form_data);
$error = '<label class="text-success">Thank you for contacting us</label>';
$name = '';
$email = '';
$subjects = '';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
form {
/* Just to center the form on the page */
margin: 0 auto;
width: 400px;
/* To see the outline of the form */
padding: 1em;
border: 1px solid #CCC;
border-radius: 1em;
}
form div + div {
margin-top: 1em;
}
label {
/* To make sure that all labels have the same size and are properly aligned */
display: inline-block;
width: 120px;
text-align: right;
}
input, textarea {
/* To make sure that all text fields have the same font settings
By default, textareas have a monospace font */
font: 1em sans-serif;
/* To give the same size to all text fields */
width: 250px;
box-sizing: border-box;
/* To harmonize the look & feel of text field border */
border: 1px solid #999;
}
input:focus, textarea:focus {
/* To give a little highlight on active elements */
border-color: #1fb;
}
textarea {
/* To properly align multiline text fields with their labels */
vertical-align: top;
/* To remove resizability feature */
resize: none;
/* To give enough room to type some text */
height: 5em;
}
.button {
/* To position the buttons to the same position of the text fields */
padding-left: 120px; /* same size as the label elements */
}
button {
/* This extra margin represent roughly the same space as the space
between the labels and their text fields */
margin-left: .5em;
}
</style>
</head>
<body>
<form method="post">
<div>
<?php echo $error; ?>
<label for="mail">E-mail:</label>
<input type="email" id="mail" name="user_mail" value="<?php echo $email; ?>">
</div>
<div>
<label for="name" id="namelabel">(Optional) Name:</label>
<input type="text" id="name" name="user_name" value="<?php echo $name; ?>">
</div>
<div>
<label for="msg">(Optional) Requested Subjects:</label>
<textarea id="msg" name="user_subjects" value="<?php echo $subjects; ?>"></textarea>
</div>
<div class="button">
<button type="submit">Submit</button>
</div>
</form>
</body>
</html>