2014-02-27 43 views
0

我有一個HTML文件有兩種形式,一種用於註冊,另一種用於登錄。 因此,當我注意到recaptcha不允許在每頁上放置更多的驗證碼時,我認爲我只能爲註冊表單或登錄表單(如果所有字段都爲空)顯示驗證碼。所以,我試圖從註冊表格開始,但它不起作用。PHP-JQuery:問題與recaptcha

HTML:

... 
<form> 
<input type="text" id="username_reg"></input> 
<input type="text" id="email_reg"></input> 
<input type="password" id="password_reg"></input> 
<input type="password" id="cpassword_reg"></input> 
<span id="captcha_reg"></span> 
</form> 
... 

JQuery的:

$(document).ready(
function() 
{ 
    $("#username_reg").change(
    function() 
    { 
    if ($('#username_reg').val()!='' && 
     $('#email_reg').val()!='' && 
     $('#password_reg').val()!='' && 
     $('#cpassword_reg').val()!='') 
    { 
    $('#captcha_reg').html('<?php require_once("recaptchalib.php");echo recaptcha_get_html("mypubkey"); ?>' 
    ); 
    } 
    } 
); 
    $("#email_reg").change(
    //same code as first 
); 
    $("#password_reg").change(
    //same code as first 
); 
    $("#cpassword_reg").change(
    //same code as first 
); 
} 
); 

我知道jQuery代碼也不是那麼優化(從查看內存資源點),但我不有介意任何其他方式來實現這一點。任何人都可以幫助我?

回答

1

我做了一次類似的事情。你想要做的是將一個隱藏的模板元素轉換爲reCaptcha小部件,然後當用戶切換到查看相應部分時,將整個小部件移動到適當的窗體div容器中。您可以在此時清除/重置您的字段,或者致電以獲取新的驗證碼挑戰,然後再向該部分的用戶展示。這裏有一個例子:

請參閱append()操作jQuery的API文檔:http://api.jquery.com/append/

HTML:

... 
<div id="login_container"> 
<form> 
<input type="text" id="username_reg"></input> 
<input type="text" id="email_reg"></input> 
<input type="password" id="password_reg"></input> 
<input type="password" id="cpassword_reg"></input> 
</form></div> 
<div id="captcha_template" style="display:none;"> 
<span id="captcha_reg"> 
     <?php 
      require_once("recaptchalib.php"); 
      echo recaptcha_get_html("mypubkey"); 
     ?> 
</span> 
</div> 
... 

JQuery的:

$(document).ready(function() { 
    // This will move the `#captcha_reg` span and its contents to the `#login_container` div when the DOM is ready. 
    $('#login_container').append($('#captcha_reg')); 
}); 
+0

但小部件移動到div容器我必須在JQuery中執行'$('#switched_from')。html('')'和$('#switched_to')。html('_reCaptcha php code _')',但是當我將reCaptcha php代碼裏面的JQuery呢不起作用。 – sleax

+1

不,你不知道。我會更新我的答案,告訴你如何。 – Derek

+0

您的recaptcha PHP代碼失敗的原因是因爲jQuery不運行PHP代碼,它運行JavaScript。因此,您需要從您的PHP/HTML視圖腳本運行您的recaptcha PHP代碼,並且只需使用jQuery在用戶採取所需操作時將呈現的元素移動到正確的容器(即,單擊登錄vs寄存器) – Derek