【问题标题】:PHP form just refreshesPHP 表单只是刷新
【发布时间】:2017-03-16 23:38:44
【问题描述】:

我已经搜索了很多问题,但没有答案。我是一个新手,我正在尝试设置一个简单的联系表格(没有数据库或任何东西)。我在网上找到了一些代码,我正在努力让它工作。我还为 reCAPTCHA 添加了代码。我只需要它来验证并发送一封电子邮件(现在给我自己)。提交后,它只是重新加载页面。我错过了什么?

<?php
if(isset($_POST['submit'])){
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "6LcM_wkUAAAAAJO-U04kp40oshoZH0gpGTjJxeox";
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);

If(isset($data->success) AND $data->success==true){

    //// True - What happens when user is verified.


/* Set e-mail recipient */
$email  = "myemailaddress80@domain.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$phone = check_input($_POST['phone'], "Enter your phone number");
$email = check_input($_POST['email'], "Enter your email");
$message = check_input($_POST['message'], "Write a brief message...");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $name
Phone: $phone
E-mail: $email

Message:
$message

End of message
";

/* Send the message using mail() function */
mail($name, $phone, $email, $message);

/* Redirect visitor to the thank you page */
header('Location: thankyou.php');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
7

    header('location: contact.php?CaptchaPass=True');

}else{

    header('location: contact.php?CaptchaFail=True');
}
}
?>

Then there is a little bit of code for reCAPTCHA

   <?php if(isset($_GET['CaptchaPass'])){ ?>
  <div> Message Sent</div> 
   <?php if(isset($_GET['CaptchaFail'])){ ?>
  <div> Captcha Failed. Please try again.</div> 
   <?php } 
   }

   ?>

Here is the form itself:

<form action="contact.php" method="post">
<p><b>Name:<br>
</b>
  <input name="name" type="text" required id="name" tabindex="1" value="" size="50" maxlength="50" />
  <br />
  Phone:<br>

  <input name="phone" type="text" id="phone" tabindex="2" value="" size="50" maxlength="50" /><br />
  <b>E-mail:</b><br>
<input name="email" type="text" tabindex="3" value="" size="50" maxlength="25" /><br />
<br>
<b>Message:</b><br />
  <textarea name="message" cols="60" rows="10" maxlength="150" tabindex="4"></textarea><br>
<br>
 <div class="g-recaptcha" data-sitekey="6LcM_wkUAAAAALpmzA-yLgvqo1xLoBRnfuZXWMf_"></div>

<br>

<p><input type="submit" value="Submit"></p>

</form>

【问题讨论】:

  • 您检查if(isset($_POST['submit'])),但您的提交按钮实际上没有名称。您需要添加以下属性:name="submit"

标签: php forms email submit


【解决方案1】:

尝试从这里取出 7,您的页面刷新可能是由您的脚本错误引起的。

} 7 header('location: contact.php?CaptchaPass=True'); }else{ header('location: contact.php?CaptchaFail=True'); }

编辑:也试试 Qirel 的建议。

【讨论】:

  • 我不知道“7”是从哪里来的。发布问题时必须添加它。它不在我的代码中。我给我的提交按钮起了个名字。还是不行。
猜你喜欢
  • 2016-12-16
  • 2018-05-31
  • 1970-01-01
  • 2014-09-24
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
相关资源
最近更新 更多