[英]PHP Email Subscription, cannot post php file
嘿,我尝试登录文本文件时遇到电子邮件订阅问题。
<!-- Signup Form -->
<form id="signup-form" action="form.php" method="post">
<input type="email" name="email" id="email" placeholder="Email Address" />
<input type="submit" value="Sign Up" />
</form>
这是我的PHP代码:
<?php
if(empty($_POST['submit']) === false) {
$email = htmlentities(strip_tags($_POST['email']));
$logname = 'email.txt';
$logcontents = file_get_contents($logname);
if(strpos($logcontents,$email)) {
die('You are already subscribed.');
} else {
$filecontents = $email.',';
$fileopen = fopen($logname,'a+');
$filewrite = fwrite($fopen,$filecontents);
$fileclose = fclose($fileopen);
if(!$fileopen or !$filewrite or !$fileclose) {
die('Error occured');
} else {
echo 'Your email has been added.';
}
}
}
?>
我一直无法获取POST /form.php事件,而php文件在该路径下,有人知道我可能做错了什么? 我为此不干了:(
首先,将名称属性添加到您的提交按钮,如在POST中一样,您将不会获得“提交”的表单值,并且您的代码将永远不会前进
<form id="signup-form" action="form.php" method="post"> <input type="email" name="email" id="email" placeholder="Email Address" /> <input type="submit" name="submit" value="Sign Up" /> </form>
现在在您的php代码中,您只需要检查$ _POST ['submit']是否符合条件,并且还输入了错字,您使用了未定义的变量$ fopen,请对其进行更改。
<?php
if($_POST['submit']) {
$email = htmlentities(strip_tags($_POST['email']));
$logname = 'email.txt';
$logcontents = file_get_contents($logname);
if(strpos($logcontents,$email)) {
die('You are already subscribed.');
} else {
$filecontents = $email.',';
$fileopen = fopen($logname,'a+');
$filewrite = fwrite($fileopen,$filecontents);
$fileclose = fclose($fileopen);
if(!$fileopen or !$filewrite or !$fileclose) {
die('Error occured');
} else {
echo 'Your email has been added.';
}
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.