我正在尝试将已注册的电子邮件附加到csv文件中,但它显示成功,而文件中没有任何内容,没有任何附加内容,而当我打印_ fwrite
它表示15
这是我的代码
<form action="send.php" method="post" name="newsletter" class="">
<label class="sr-only">Email:</label>
<div class="input-group mb-2">
<input type="email" name="email" class="form-control rounded-right bg-transparent" placeholder="johndoe@example.com" aria-label="Username" aria-describedby="basic-addon1" style="border: 1px solid #bdbdbd;">
<button type="submit" class="home-signup-email-btn btn btn-blue btn-lg ml-3 rounded py-0">Sign Up</button>
</div>
</form>
$email = $_POST['email'];
//$filename = 'suscribers.txt';
//$somecontent = "$email\n";
// Let's make sure the file exists and is writable first.
$txt = '/n'.$_POST['email'].'/n';
$file = fopen('mailing_list.csv','a');
if (fwrite($file,$txt)){
// fwrite($file,$txt);
$message= "Success!. You have been added to our email list.";
$status = "success";
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);}
else
echo "fail";
答案 0 :(得分:0)
尝试使用:
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
答案 1 :(得分:0)
使用+模式:
打开文件进行读/写。保留文件中的现有数据。文件指针从文件末尾开始。如果文件不存在,则创建新文件
<form action="send.php" method="post" name="newsletter" class="">
<label class="sr-only">Email:</label>
<div class="input-group mb-2">
<input type="email" name="email" class="form-control rounded-right bg-transparent" placeholder="johndoe@example.com" aria-label="Username" aria-describedby="basic-addon1" style="border: 1px solid #bdbdbd;">
<button type="submit" class="home-signup-email-btn btn btn-blue btn-lg ml-3 rounded py-0">Sign Up</button>
</div>
</form>
$email = $_POST['email'];
// Let's make sure the file exists and is writable first.
$txt = '/n'.$_POST['email'].'/n';
$file = fopen('mailing_list.txt','a+');
if (fwrite($file,$txt)){
$message= "Success!. You have been added to our email list.";
$status = "success";
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
fclose($file);
chmod($file, 0777);
}else {
echo "fail";
}
答案 2 :(得分:0)
我找到了一个解决方案,它没有写入文件,因为我没有添加fclose
来关闭fopen