有人使用了我的代码,并说对他们有用,但是由于某些原因,在我的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