固定
我的代码中出现了一个小错误,我更改了"类型" to" text"不知何故,在电子邮件输入中,应该是
type="email"
和不是
type="text"
我已经制作了一个php联系表格,我在我制作的网站上使用,一切正常。
我认为表单正在检查已输入的电子邮件地址,但我现在意识到人们可以将他们的名字放在电子邮件输入中,而不是他们的完整电子邮件地址(即使用@符号和.com或.co。英国最后)
我在PHP上的表现并不是很好,所以尽管我添加了一些声明,但我仍然没有提供有效的电子邮件地址的电子邮件似乎仍然存在。
我尝试定义一些错误变量并使用空值设置如下:
$nameErr = $emailErr = $genderErr = $websiteErr = "";
然后将这些if else语句添加到我的表单php:
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
所以我的代码如下:
<?php
$name = ($_POST['name']);
$email = ($_POST['email']);
$message = ($_POST['message']);
$from = ($_POST['email']);
$to = 'me@myemailaddress.co.uk';
$subject = "Enquiry from Visitor " . $name;
$human = ($_POST['human']);
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
?>
<div class="container-fluid">
<div class="about-msg">
<h2>CONTACT US</h2>
<div class="container-fluid contactform">
<div class="col-md-7 contactform-padding">
<br>
<h1>GET IN TOUCH</h1>
<p>Please drop us a message if you have any enquires. We always aim to reply
within 24 hours.</p>
<?php
if (isset($_POST['submit']) && $human == '4') {
if (mail ($to, $subject, $message, $headers)) {
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
echo '<p>Thanks for getting in touch. Your message has been sent & we
will get back to you shortly!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
<form method="post" action="contact-us.php" data-ajax="false" method="POST"
enctype="multipart/form-data">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label " for="name">
<b>NAME</b>
</label>
<input class="form-control" id="name" name="name" type="text"
placeholder="name">
</div>
<div class="form-group col-md-6">
<label class="control-label requiredField" for="email">
<b>EMAIL</b>
<span class="asteriskField">
*
</span>
</label>
<input class="form-control" id="email" name="email" placeholder="email"
type="text"/>
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="control-label requiredField" for="contact-subject">
<b>SUBJECT</b></label>
<input type="text" name="subject" class="contact-subject form-control"
id="contact-subject" placeholder="subject">
</div>
</div>
<div class="form-group">
<label class="control-label " for="message">
<b>MESSAGE</b>
</label>
<textarea class="form-control" cols="40" id="message" name="message"
rows="10" style="height: 275px !important;"></textarea>
</div>
<div class="form-group">
<label><b>*What is 2+2? (Anti-spam)</b></label>
<input name="human" placeholder="Type Here"></div>
<br>
<button class="btn btn-success" name="submit" type="submit" value="submit">
SEND
</button>
</form>
</div>
然而,这让我发送电子邮件,尽管我的电子邮件地址只是我的名字!
有人可以看看我是否已将代码放在正确的位置或提出如何修复它的建议?我刚刚学习PHP,所以我可能在我的代码中出错或者可能需要将if语句用于提交?
感谢您的帮助!
答案 0 :(得分:3)
$email_b = "me@myemailaddress.co.uk";
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_b) email address is considered valid.\n";
} else {
echo "This ($email_b) email address is considered invalid.\n";
}
答案 1 :(得分:2)
我想您希望用户输入有效的电子邮件地址,对吧? 您可以将类型更改为电子邮件
<input class="form-control" id="email" name="email" placeholder="email" type="email"/>
答案 2 :(得分:-2)
那么你可以选择regex
,但这也是option