PHP form just refreshes

2019-09-14 19:20发布

问题:

I have searched for this with many questions but no answers for me. I'm a newbie and I'm trying to setup a simple contact form (no database or anything). I found some code online and I'm trying to get it to work. I added code for reCAPTCHA also. I just need it to validate and send an email (to myself right now). After submit, it just reloads the page. What am I missing?

<?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:

Try taking the 7 out from here, your page refreshing could be caused by your script erroring.

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

EDIT: Also try what Qirel suggested.