我正在编写一个简单的PHP订阅表单,当它运行时它没有做任何事情。 PHP看起来像这样:
object
如果我提交没有内容或格式不正确的内容的表单,并且没有写入文件,它就不会丢失错误,当我按下提交内容时它似乎没有做任何事情按钮(虽然它确实清除了字段并且似乎提交了表单) 它可能很简单,但我的眼睛完全没有它 任何帮助将不胜感激 提前致谢, 扎克
答案 0 :(得分:2)
正确关闭表单标记然后将php代码移到页面顶部。这样的东西应该有效。
<?php
$email = '';
$inError = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (empty($_POST['email']))
$inError = "You must provide an email to register!";
elseif (stripos($_POST['email'], '@') !== TRUE || stripos($_POST['email'], '.') !== TRUE)
$inError = "Please provide a properly formatted email to register.";
else {
$email = sanatizeInput($_POST['email']);
$emailFile = fopen('emails.txt', 'a') or die('Sorry. Subscriptions are disabled for the time being.');
fwrite($emailFile, $email);
}
}
function sanatizeInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form class='subscribe' method='post' action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>'>
Subscribe for occasional updates and reminders!
Email: <input type='text' class='input' name='email' id='email' >
<!--There will be some type of CAPTCHA here-->
<label for='Subscribe'>
<input value='Subscribe' type='submit' id='subscribebutton' class='input' name='subscribebutton'>
</label>
<?php if(!empty($inError)){ ?><span class='error'><?php echo $inError; ?> </span> <?php } ?>
</form>