2016-02-29 32 views
1

我試圖確保在發送包含我的表單數據的電子郵件表單之前填充google recaptcha2。然而,由於基於JavaScript的電子商務需要我的網站的功能,表單數據首先傳遞給一個JavaScript文件,然後作爲另一種形式發佈到PHP文件。我試圖找到一種方法來使用g-recaptcha-response,並從原始表單到javascript表單到php sendmail文件,以驗證它已經以普通的後端方式填充。通過從JavaScript文件傳遞g-recaptcha-response到php文件來驗證google recaptcha

首先剝離HTML,所以你可以看到我在正確的地方的一切,網站的重點由「MySiteKey」補充:

<head><script src='https://www.google.com/recaptcha/api.js'></script></head> 

<form name="form" id="form"> 
<fieldset> 
<ol>  
<li><label for="emaile">Email Address: </label> 
<input type="email" placeholder="&nbsp;Email Address" name="emaile" class="input" required id="emaile" /></li> 
</ol> 

<div class="g-recaptcha" data-sitekey="MySiteKey"></div> 
<button class="submit" type="submit" name="Submit" style="cursor:pointer" formaction="javascript:simpleCart.checkout()">Submit</button> 
</fieldset> 
</form> 

下一頁外部JS的大量簡化,但相關的部分文件

simpleCart.checkout() 

emaile = document.form.emaile.value; 

var b = document.createElement("form"); 
       b.style.display = "none"; 
       b.method = "POST"; 
       b.action = "/php/sendjs.php"; 
       b.acceptCharset = "utf-8"; 

b.appendChild(a.createHiddenElement("emaile", emaile)); 

b.appendChild(a.createHiddenElement("g-recaptcha-response", g-recaptcha-response)); 

document.body.appendChild(b); 
       b.submit(); 
       document.body.removeChild(b) 

我想追加「G-驗證碼 - 響應」字符串傳遞給同其他形式的數據的單獨的PHP文件,以檢查是否驗證碼被髮送之前填充:

<?php 
$captcha = $_POST['g-recaptcha-response']; 
$secretKey = "MySecretSiteKey "; 
$ip = $_SERVER['REMOTE_ADDR']; 
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify? secret=".$secretKey."&response=".$captcha."&remoteip=".$ip); 
$responseKeys = json_decode($response,true); 
if(intval($responseKeys["success"]) !== 1) { 
echo '<h2>NOT SENT</h2>'; 
} else { 
$subject = 'Order Inquiry from DIY Soakwells'; 
$time = date ("h:i A"); 
$date = date ("l, F jS, Y"); 
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
$headers .= 'From: [email protected]' . "\r\n" . 
'Reply-To: [email protected]' . "\r\n" . 
'X-Mailer: PHP/' . phpversion(); 

$emaile = $_POST['emaile']; 

$to = "[email protected],$emaile"; 
$text = "<html><body><p><b>Customers Email Address:</b> $emaile</p> 
</html>   
</body>"; 
$body = $text; 
mail($to, $subject, $body, $headers); 
Header('Location: ../form-accepted.php'); 
    } 
?> 

一切工作,除了檢查recaptcha是填充,我明白我在這裏不會工作的方式,但我試圖說明我的方法,我一直在努力幾個小時才能讓它工作到無濟於事,任何幫助將不勝感激。希望有人能夠理解我正在嘗試做什麼,謝謝。

此鏈接顯示部分2 http://webdesign.tutsplus.com/tutorials/how-to-integrate-no-captcha-recaptcha-in-your-website--cms-23024

的鍵/值對我非常開放的替代的建議。

+0

「https://www.google.com/recaptcha/api/siteverify?secret」< - 刪除空格? – Kordi

+0

不幸的是,這不是問題,當我粘貼問題的代碼時可能有空白。驗證檢查在php文件中工作,我只是不知道如何獲取鍵/值對,以允許它發送。它在谷歌瀏覽器(js文件)的檢查員中說「g-recaptcha-response未定義」。 – JPB

回答

1

這應該解決所有問題。

function createHiddenElement(name, value) { 
    var input = document.createElement("input"); 
    input.setAttribute("type", "hidden"); 
    input.setAttribute("name", name); 
    input.setAttribute("value", value); 
    return input; 
} 

simpleCart.checkout() 


var b = document.createElement("form"); 
b.style.display = "none"; 
b.method = "POST"; 
b.action = "/php/sendjs.php"; 
b.acceptCharset = "utf-8"; 

b.appendChild(createHiddenElement('emails', document.form.emaile.value)); 
b.appendChild(createHiddenElement('g-recaptcha-response', document.getElementById('g-recaptcha-response').value)); 

document.body.appendChild(b); 
b.submit(); 
document.body.removeChild(b) 
+0

感謝隊友,我必須在早上檢查一下,在澳大利亞這裏遲到。手指交叉,會盡快回復你。 – JPB

+0

確定沒有錯誤,直到我運行js,然後它在控制檯中顯示「Uncaught ReferenceError:g未定義」。它不像g-recaptcha-response中的「 - 」。我如何獲得該字符串並POST? – JPB

+1

現在有一個完整的新imlpementation – Kordi