我的页面上有评论/问题表格。在我实施“提交”之前代码,它会检查验证工作。现在,每次我点击提交,它都会转到我发送的消息'页面,即使我知道它不会页面验证。
我已经检查了一些相关的答案,但无法得到这个答案......
*注意:删除了一些值以进入主要点
主要PHP:
<section style="-webkit-flex-direction: column; flex-direction: column;">
<?php include '/home/ubuntu/workspace/commentform.php';?>
<h2>Questions & Concerns</h2><br>
<p><span class="error">* required field</span></p><br>
<form name="contactform" method="post" action="comment-handler.php">
Full Name: <input type="text" name="name" value="<?php echo $name;?>" style="width:22em">
<span class="error">* <?php echo $nameErr;?></span><br><br>
Phone Number: (xxx) xxx-xxxx <input type="text" name="phone" value="<?php echo $phone;?>" style="width:9.5em"><br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>" style="width: 25em">
<span class="error">* <?php echo $emailErr;?></span><br><br>
Comment:<br><sub>*500 Character Maximum.</sub>
<textarea name="comment" size="500" maxlength="500" rows="10" column="50" style="width: 50em; height: 10em"><?php echo $comment;?></textarea><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</section>
错误处理表单
<?php
$nameErr = $phoneErr = $emailErr = $invoiceErr = "";
$name = $phone = $email = $invoice = $response = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["phone"])) {
$phone = "";
} else {
$phone = test_input($_POST["phone"]);
}
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["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
评论 - 处理程序(提交 - 行动)表格:
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$invoice = $_POST['invoice'];
$callResponse = $_POST['callResponse'];
$textResponse = $_POST['textResponse'];
$emailResponse = $_POST['emailResponse'];
$comment = $_POST['comment'];
$content = "Name: $name \n\nPhone: $phone \n\nEmail: $email \n\nInvoice #: $invoice \n\nContact Preference(s):\nCall: $callResponse Text: $textResponse Email: $emailResponse \n\n Question/Concern:\n\n $comment";
ini_set('display_errors',1);
if(isset($_POST['submit'])){
$to = '7servicerepairs@gmail.com';
$subject = "New Question For Se7en Service!";
$txt = $content;
$headers = array("From: info@se7enservice.com", "Reply-To: $email");
$headers = implode("\r\n", $headers);
mail($to,$subject,$txt,$headers);
if(mail($to,$subject,$txt,$headers)) {
include '/home/ubuntu/workspace/sentMessage.php';
} else {
echo '<p>Message DID NOT Sent. Please Try Again.</p>';
}
}
?>
答案 0 :(得分:1)
这应该可以解决问题......如果我遗失了什么,请告诉我?或者你需要帮助理解它?
预览:
// PHTML
<?php
if(!empty($_POST)){
$POST = filter_post($_POST);
$MSG = check_empty($POST);
if(!array_filter($MSG)){
if(send_mail($POST)){
$MSG[] = "Email Success";
}
else{
$MSG[] = "Email Failed";
}
}
}
function filter_post($POST){
$keys = array('name','phone','email','comments');
$POST = array_intersect_key($POST, array_flip($keys));
$POST = array_map('strip_tags', $POST);
return($POST);
}
function check_empty($POST){
foreach($POST as $key => $value){
if(empty($value)){
$MSG[] = "You need to fill out the $key section";
}
}
return($MSG);
}
function send_mail($POST){
extract($POST);
$to = '7servicerepairs@gmail.com';
$sbj = 'New Question For Se7en Service!';
$msg = "Name = $name \n Phone = $phone \n Email = $email \n Comments = $comments";
$headers = "From: $email";
return(mail($to, $sbj, $msg, $headers));
}
?>
<!DOCTYPE html>
<html>
<head>
<link href="index.css" rel="stylesheet">
</head>
<body>
<form method="POST" action="">
<input type="text" name="name" placeholder="Name" value="<?php echo $_POST['name']; ?>">
<input type="tel" name="phone" placeholder="Phone Number" value="<?php echo $_POST['phone']; ?>">
<input type="email" name="email" placeholder="Email" value="<?php echo $_POST['email']; ?>">
<textarea name="comments" maxlength="500" rows="10" cols="10" placeholder="Please enter your comments here..."></textarea>
<button type="submit">Submit</button>
</form>
<mark><?php echo $MSG[0]; ?></mark>
</body>
</html>
// CSS
body{
margin: 0 !important;
width: 100vw;
height: 100vh;
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-wekbit-flex-direction: column;
flex-direction: column;
background: #1E67CB;
}
form{
cursor: default !important;
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-align-self: center;
align-self: center;
-webkit-flex-direction: column;
flex-direction: column;
background: #ECF0F1;
-webkit-box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
-webkit-border-radius: 0.3em;
border-radius: 0.3em;
padding: 1.3em;
}
form>input{
width: 22.1em;
height: 2.5em;
margin: 0.2em 0;
font-size: 1em;
-webkit-font-smoothing: antialiased;
font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
text-align: center;
border: 1px solid #d5dadc;
-webkit-border-radius: 2px;
border-radius: 2px;
color: #7C7C7C;
outline: none;
}
form>button{
width: 22.35em;
height: 2.5em;
margin: 0.2em 0;
padding: 0;
font-size: 1em;
-webkit-font-smoothing: antialiased;
font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
cursor: pointer;
outline: none;
border: none;
color: #fff;
background: #2ECC71;
-webkit-border-radius: 2px;
border-radius: 2px;
}
form>textarea{
margin: 0.2em 0;
font-size: 1em;
-webkit-font-smoothing: antialiased;
font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
border: 1px solid #d5dadc;
-webkit-border-radius: 2px;
border-radius: 2px;
color: #7C7C7C;
outline: none;
max-width: 22em;
}
mark{
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-align-self: center;
align-self: center;
height: 1.5em;
margin: 1.5em;
font-size: 1em;
color: #fff;
-webkit-font-smoothing: antialiased;
font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
background: none;
}