2016-11-03 14 views
0

私は多くの質問でこれを検索しましたが、答えはありません。私は初心者で、簡単な連絡フォーム(データベースや何もない)をセットアップしようとしています。私はいくつかのコードをオンラインで見つけました。私はそれを動作させようとしています。私はreCAPTCHAのコードも追加しました。私はただそれを検証し、メールを送る必要があります。送信後、ページをリロードするだけです。私は何が欠けていますか?PHPフォームがちょうどリフレッシュ

<?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 = "[email protected]"; 

/* 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> 
+0

あなたは 'if(isset($ _ POST ['submit']))')をチェックしますが、submit-buttonは実際に名前を持っていません。次の属性を追加する必要があります: 'name =" submit "' – Qirel

答えて

0

ここから7を取り上げてみてください。あなたのページがリフレッシュされる原因は、スクリプトのエラーです。

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

EDIT:またQirelが提案何してみてください。

+0

「7」の出所はわかりません。質問を投稿するときに追加しておく必要があります。それは私のコードではありません。送信ボタンに名前を付けました。まだ行きません。 – Newsong80

関連する問題