php - 表单提交未附加到 CSV
问题描述
有人使用了我的代码并说它对他们有用,但由于某种原因在我的 Mac 上,每当我使用我的表单时,它都不会附加到我目录中的 CSV。它是一个名为 data.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"));
$form_data = array(
0 => $name,
1 => $email,
2 => $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>
<?php echo $error; ?>
<form method="post">
<div>
<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" name="submit">Submit</button>
</div>
</form>
</body>
</html>
如果您需要,这是我的目录的屏幕截图: https ://imgur.com/a/nYNRH0i
解决方案
推荐阅读
- redux - 在 redux 中观察 redux-saga 动作
- google-bigquery - 使用源谷歌驱动器创建表模式并在 bigquery 表中加载数据
- service - 无法设置服务的优先级
- swift - 如何使用 SwiftUI 的 onDelete() 函数访问数组位置
- python - 用 OpenCV + Python 拼接四个图像
- swift - ScrollView 在关闭 ViewController 并调用 viewDidLoad 时不会重新加载
- python - 从网站抓取价格为零的问题
- python - 如何遍历 UnqLite 集合并提取数据
- flutter - 用于颤振的 Chopper 库时请求中的内容类型被覆盖
- vhdl - if 会产生其他支持吗?