PHP表单只是刷新

时间:2016-11-03 11:42:10

标签: php forms email submit

我搜索过很多问题,但没有答案。我是一个新手,我试图设置一个简单的联系表格(没有数据库或任何东西)。我在网上找到了一些代码,并且我试图让它运行起来。我还为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>

1 个答案:

答案 0 :(得分:0)

尝试从此处取出7,您的页面刷新可能是由于脚本错误造成的。

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

编辑:同时尝试Qirel建议的内容。