简体   繁体   English

PHP:如果表单中的必填字段未填写,如何使用户停留在同一页面上?

[英]PHP: How to make the user stay on the same page if the required fields in the form aren't filled?

How can I make the user stay on the same page if the required fields in the form aren't filled? 如果表单中的必填字段未填写,如何使用户停留在同一页面上? Any answer would be greatly appreciated. 任何答案将不胜感激。

<?php 
header('Location: COMING SOON.html');
$name = $_POST['name'];
$surname = $_POST['last_name'];
$cons = $_POST['cons'];
$password = $_POST['pass'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "pixiedustmed@yahoo.com";

$to = 'pixiedustmed@yahoo.com';

$subject = "Contact Message from: $name $surname - $email";

$headers = "From: $name $surname \r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "CC: avedis@avedis.ga\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= '<div class="about-center"><h1 style="font-family: Lato-Light; font-size: 2em">You have received a contact message from Your website.</h1></div>';
$message .= '<table rules="all" style="border-color: #666; font-family: Segoe UI; font-size: 16px;" cellpadding="12">';
$message .= "<tr style='background: #eee;'><td><p style='color: #2c3e50; font-weight:bold'>Name: </p> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr style='background: #eee;'><td><p style='color: #2c3e50; font-weight:bold'>Surname: </p> </td><td>" . strip_tags($_POST['last_name']) . "</td></tr>";
$message .= "<tr><td><p style='color: #2c3e50; font-weight:bold'>Email: </p> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr style='background: #71bdf4;'><td><p style='color: #ffffff'>Message: </p> </td><td>" . strip_tags($_POST['message']) . "</td></tr>";
$message .= "<tr style='background: #34495e;'><td><p style='color: #ffffff'>Consultation Info: </p> </td><td style='color: #ffffff'>" . strip_tags($_POST['cons']) . "</td></tr>";
$message .= "<tr><td><p style='color: #2c3e50; font-weight:bold'>Password: </p> </td><td>" . strip_tags($_POST['pass']) . "</td></tr>";
$message .= "</table>";
$message .= '<br><h1 style="font-family: Lato-Light; font-size: 14px; color:#c0392b;"><a href="http://avedis.ga">Click here to open avedis.ga</a></h1>';
$message .= '<h1 style="font-family: Segoe UI; font-size: 13px; color:#eee;">This is an autogenerated message designed by S.G.</h1>';
$message .= '<div class="about-center">-</div>';
$message .= "</body></html>";


mail($recipient, $subject, $message, $headers, $password) or die("Error!");
echo "Thank You!";
?>

So, as you see, I have put header('Location: COMING SOON.html'); 因此,如您所见,我已经放置了header('Location: COMING SOON.html'); which evantually works. 这是行得通的。 Plus, in my css, I have put a required="" field which didn't let the user click the submit button and displayed a message. 另外,在我的CSS中,我放置了一个required=""字段,该字段不允许用户单击“提交”按钮并显示一条消息。 But now that I put a header, the PHP disregards everything. 但是,既然我放了头文件, PHP忽略一切。

My site can be accessed by clicking here ; 单击此处可以访问我的网站; scroll down to the end of the page and try in the contact form, and. 向下滚动到页面末尾,然后尝试使用联系表,然后。 Let me know what you think about the design and all! 让我知道您对设计及所有方面的看法!

if(empty($name) || empty($surname) || empty($cons) || empty($password) || empty($email) || empty($_POST['email']) || empty($message)){
    // do something if a field is empty
} else {
    // do something if fields are all filled
}

Put this after you declare the variables like $name , $surname , and $cons . 在声明诸如$name$surname$cons类的变量之后,将其放入。

Use some javascript to ensure that the user has filled all the fields, or just include the php inside the index page and if the user has filled all the forms send the mail, otherwise show an error around the unfilled fields. 使用一些JavaScript来确保用户填写了所有字段,或者仅将php包含在索引页面内,并且如果用户填写了所有表格,则发送邮件,否则在未填写的字段周围显示错误。

Example using jQuery: Check all input fields have been filled out with jQuery 使用jQuery的示例: 检查所有输入字段是否已用jQuery填写

You should check and validate the feilds first and if the feilds are correctly submitted, then only u should redirect the user using the header(). 您应该首先检查并验证平台,如果平台提交正确,则只有u可以使用header()重定向用户。

ps: what you asked was actually not clear. ps:您的要求实际上不清楚。

Hope it helps thankz! 希望它可以帮助谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM