【发布时间】:2018-05-14 15:59:22
【问题描述】:
我一直在处理 php 文件中的表单。正如您将在下面的代码中看到的那样,我已经将所有内容都写在了一个 PHP 文件中,包括表单标签。这个 form.php 文件是从我的文件夹中名为contact.php 的另一个源调用的。我承认这是最整洁的书面代码。但是由于某种原因,当我在此表单上提交我的信息时,它告诉我该表单已提交。然而,我的 billy.farroll@hotmail 电子邮件地址没有收到包含任何提交信息的电子邮件 ($email_to)。它也适用于谷歌recaptcha,如果有任何无效输入。只是由于某种原因,它不会通过数据发送给我,即姓名、电子邮件、联系电话或查询。
非常感谢您的帮助。
我的内联 CSS 样式:
<style>
form span{
color: red;
}
form input[type="submit"]{
background: url('images/logo2.png') no-repeat;
background-size: contain;
overflow: visible;
padding-left: 45px;
height: 20px;
border: 0px;
margin: 0 auto;
margin-top: 5px;
font-family: 'Orbitron', sans-serif;
text-transform: uppercase;
}
.g-recaptcha {
margin: 5px;
}
</style>
我的 PHP
<?php
if(strpos($_SERVER['REQUEST_URI'], 'form' )!== false){
header('Location:index.php');
}
if($_POST){
$email;
$name;
$captcha;
$telephone;
$question;
if(isset($_POST['email']))
$email=$_POST['email'];
if(isset($_POST['name']))
$name=$_POST['name'];
if(isset($_POST['telephone']))
$telephone=$_POST['telephone'];
if(isset($_POST['message']))
$question=$_POST['message'];
if(isset($_POST['g-recaptcha-response']))
$captcha=$_POST['g-recaptcha-response'];
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) || ($name == "") ||
($question == "") ||(!$captcha))
{
echo "<p>Invalid Input.</p>";
?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>
method="post" style="text-align:left;">
<label>Name: <span>*</span></label><input name="name" type="text" />
<label>Email: <span>*</span></label><input name="email" type="text" />
<label>Contact Number: </label><input name="telephone" type="text" />
<label>Enquiry: <span>*</span></label>
<textarea name="message" rows="10" cols="30"></textarea>
<div class="g-recaptcha" data-sitekey="<!-- SITEKEY -->"></div>
<input type="submit" value="submit" />
</form>
<?php
} else {
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=<!-- SECRET KEY -->=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false)
{
echo '<h2>Spam detected</h2>';
}
else {
$email_from = $_POST["email"];
$email_to = "billy.farroll@hotmail.com";
$question = $_POST["message"];
$email_subject = "Enquiry";
$headers =
"From: $email_from \n";
"Reply-To: $email_from \n";
$message =
"Name: ". $name .
"\r\nMobile Number: " . $telephone .
"\r\nEmail Address: " . $email .
"\r\n\r\n\r\n" .
"\r\n\r\nMessage: \r\n" . $question;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);
if ($sent)
{
echo 'Thank you for your enquiry, one of our team will be in contact with you shortly.';
}
}
}
} else {
?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post" style="text-align:left;">
<label>Name: <span>*</span></label><input name="name" type="text" />
<label>Email: <span>*</span></label><input name="email" type="text" />
<label>Contact Number: </label><input name="telephone" type="text" />
<label>Enquiry: <span>*</span></label>
<textarea name="message" rows="10" cols="30"></textarea>
<div class="g-recaptcha" data-sitekey="<!-- SITEKEY -->"></div>
<input type="submit" value="submit" /><span> * required fields</span>
</form>
<?php } ?>
【问题讨论】:
-
嗯,试试
action="" -
如果您完全删除
action = "",表单将默认操作为$_SERVER["PHP_SELF"] -
我要么用双引号将
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>括起来,要么完全去掉它。没有任何操作的表单将始终回发给自身。如果您仍然遇到问题,请尝试将 var_dump($_POST) 添加到您的 PHP 脚本中,看看您是否收到任何发布的内容。 -
已经尝试完全删除操作并添加双引号,即 action="" 都不起作用,还有其他解决方案吗?请。