【发布时间】:2015-08-24 23:55:16
【问题描述】:
我以前在 Python 编程方面有一些经验,但我被要求为某人构建一个网页,所以我尝试使用引导框架、html 和 php 构建一个基本页面(边学习边学习)。
从没有验证的订阅表单来看,一切似乎都完全符合需要,所以我认为最好隐含一些,但表单似乎没有反应,仍然允许脚本完成并发送电子邮件,即使有完全空白的表格。
这会是因为表单处于“模态”吗?在这方面我还是个新手,如果之前已经介绍过,我深表歉意,但我找不到对我有帮助的答案。
我附上模态表单html和php代码。
<!-- Modal form -->
<div class="modal fade" id="Subscribe" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Subscribe for updates!</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<form method="post" action="Submit.php">
<div class="row">
<div class="col-xs-12">
<div class="input-group" style="margin-bottom: 5px;">
<span class="input-group-addon" id="first">First Name</span>
<input type="text" class="form-control" placeholder="John" name="first" aria-describedby="First-Name">
<span class="input-group-addon error" style="color: red">* <?php echo $firstErr;?></span>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="input-group" style="margin-bottom: 5px;">
<span class="input-group-addon" id="last">Surname</span>
<input type="text" class="form-control" placeholder="Smith" name="surname" aria-describedby="Surname">
<span class="input-group-addon error" style="color: red">* <?php echo $lastErr;?></span>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="input-group" style="margin-bottom: 5px;">
<span class="input-group-addon" id="email">Email</span>
<input type="email" class="form-control" placeholder="something@example.co.uk" name="email" aria-describedby="Email">
<span class="input-group-addon error" style="color: red">* <?php echo $fromErr;?></span>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="input-group" style="margin-bottom: 5px;">
<span class="input-group-addon" id="notes">Notes</span>
<textarea class="form-control" rows="3" placeholder="Please put anything else you want to say here" name="notes" aria-describedby="Notes">
</textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" name="submit" value="submit" class="btn btn-primary">Send</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
名为“Submit.php”的文件
<?php
// define variables and set to empty values
$fromErr = $firstErr = $lastErr = $notesErr = "";
$from = $first_name = $last_name = $notes = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['submit'])) {
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["first"])) {
$firstErr = "Name is required";
} else {
$first_name = test_input($_POST["first"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$firstErr = "Only letters and white space allowed";
}
}
if (empty($_POST["surname"])) {
$lastErr = "Surname is required";
} else {
$last_name = test_input($_POST["surname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
$lastErr = "Only letters and white space allowed";
}
}
$notes = test_input($_POST["notes"]); // this is the senders message
$to = "myemail@domain.com"; // this is your Email address
$subject = "Website subscription from " . $first_name . " " . $last_name; //Subject line
$message = "First name: " . $first_name . "<br>" . "Surname: " . $last_name . "<br>" . "Email: " . $from . "<br>" . "Notes: " . $notes;
$headers = "From: " . $from . "\r\n";
// $headers .= "CC: myemail@domain.comr\n"; option to CC
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);
header('Location: ./index.html#Thanks'); // redirect back to main page with 'Thanks' Modal open
}
?>
我将非常感谢任何帮助!
编辑:
以防有人想知道我是如何最终到达我需要的位置:@leepowers 的回答是朝着正确方向迈出的一大步,这使我能够检查是否有任何错误并阻止电子邮件发送(如果有) ,然后我了解了“会话”,我将错误存储在会话中(可能以非常混乱的方式),因此我可以在返回表单时重复使用它们。
// Gather all errors into an array
$errors = array($fromErr, $firstErr, $lastErr);
// Remove any empty error messages from the array
$errors = array_filter($errors);
// An array with more than zero elements evaluates `true` in a boolean context
if ($errors) {
$_SESSION["fromErr"] = $fromErr;
$_SESSION["firstErr"] = $firstErr;
$_SESSION["lastErr"] = $lastErr;
die(header('Location: ./index.html#Subscribe'));
} else { // send email
在我的 html 页面中,我有一些 JS 将“重定向”定向到模态:
<script>
$(document).ready(function() {
if(window.location.href.indexOf('#Thanks') != -1) {
$('#Thanks').modal('show');
} else if(window.location.href.indexOf('#Subscribe') != -1) {
$('#Subscribe').modal('show');
}
});
</script>
现在我在表单中的每个输入都有一个专门的错误:
<div class="input-group" style="margin-bottom: 5px;">
<span class="input-group-addon" id="email">Email</span>
<input type="email" class="form-control" placeholder="something@example.co.uk" name="email" aria-describedby="Email">
<span class="input-group-addon error" style="color: red">* <?php echo $_SESSION["fromErr"];?></span>
谢谢大家的帮助。
【问题讨论】:
-
你没有对
$lastErr或$firstErr做任何事情,所以电子邮件只是发送。尝试添加一个条件,要求在mail($to,$subject,$message,$headers);之前都为空。 -
我不确定其他人是否这样做,但我通常将所有错误附加到一个数组中,然后检查在检查多个条件时是否发生任何错误,只需执行
if (count($errors) > 0) { handleError(); } else { doSomething(); } -
另外,
test_input()到底是做什么的? -
@Mike 函数定义在顶部
-
@andrew 啊,你是对的。我以某种方式错过了它。 @OP:除非您运行 PHP magic_quotes_gpc 设置为
true,否则您可能应该摆脱stripslashes()。
标签: php jquery html twitter-bootstrap validation