2012-01-13 24 views
1

首先,我用下面的代碼生成通過JavaScript形式,並將它附加一些容器(我的選擇):PHP&JavaScript |包括PHP數學驗證碼在JavaScript生成的表單

var registerStructureWrapper = '<div class="content-register"></div>'; 

var registerStructure = [ 
     '<form name="',opts.regFormClass,'" class="',opts.regFormClass,'" method="post" action="#">', 
      '<fieldset class="',opts.regFieldsWrapper,'">', 
       '<fieldset class="',opts.regUserWrapper,'">', 
        '<label for="',opts.regUserInt,'" class="',opts.regUserLbl,'">',checkNameLenght(opts.regUserName,regNamesLenght.userNameLenght,20,'Username'),'</label>', 
        '<input type="text" name="',opts.regUserInt,'" class="',opts.regUserInt,'" placeholder="" value="" autocomplete="off" />', 
       '</fieldset>', 
       '<fieldset class="',opts.regEmailWrapper,'">', 
        '<label for="',opts.regEmailInt,'" class="',opts.regEmailLbl,'">',checkNameLenght(opts.regEmailName,regNamesLenght.emailNameLenght,20,'Email'),'</label>', 
        '<input type="email" name="',opts.regEmailInt,'" class="',opts.regEmailInt,'" placeholder="" value="" autocomplete="off" />', 
       '</fieldset>', 
       '<fieldset class="',opts.regPassWrapper,'">', 
        '<label for="',opts.regPassInt,'" class="',opts.regPassLbl,'">',checkNameLenght(opts.regPassName,regNamesLenght.passNameLenght,20,'Password'),'</label>', 
        '<input type="password" name="',opts.regPassInt,'" class="',opts.regPassInt,'" placeholder="" value="" autocomplete="off" />', 
       '</fieldset>', 
       '<fieldset class="',opts.regConfWrapper,'">', 
        '<label for="',opts.regConfInt,'" class="',opts.regConfLbl,'">',checkNameLenght(opts.regConfName,regNamesLenght.confNameLenght,20,'Confirm Password'),'</label>', 
        '<input type="password" name="',opts.regConfInt,'" class="',opts.regConfInt,'" placeholder="" value="" autocomplete="off" />', 
       '</fieldset>', 
       '<fieldset class="',opts.regBtnWrapper,'">', 
        '<div class="message-handling"></div>', 
        '<button type="submit" name="',opts.regBtnInt,'" class="',opts.regBtnInt,'">',checkNameLenght(opts.regBtnName,regNamesLenght.btnNameLenght,8,'Register'),'</button>', 
       '</fieldset>', 
      '</fieldset>', 
     '</form>', 
     '<div class="',opts.regBackBtn,'">', 
      '<ul class="inside">', 
       '<li class="back"><a><img src="assets/gfx/back.png" alt="Back" /></a></li>', 
      '</ul>', 
     '</div>' 
    ]; 


    $(registerStructureWrapper).appendTo(container).hide(); 
    var registerWrapper = $(container).find('.content-register'); 
    $(registerStructure.join('')).appendTo(registerWrapper); 

和我有下面的代碼生成一個數學驗證碼:

<?php 

    public function mathCaptcha() 
    { 
     $sum1 = mt_rand(1,9); 
     $sum2 = mt_rand(1,9); 
     $sum3 = $sum1 + $sum2; 
     $_SESSION['answer'] = $sum3; 
     return $sum1 . ' + ' . $sum2 . ' = '; 
    } 

?> 

我的問題是我能以某種方式包括總和爲JS生成的表單?有沒有一種方法可以做到這一點,而沒有在純HTML中的形式,但生成它,因爲我已經這樣做?

提示:該腳本不在頁面上,它是一個帶有<script>標籤的加載腳本。

+0

只是clerafication ..你知道,php是在服務器端執行和HTML和Javascript在clint端執行? – reporter 2012-01-13 11:39:13

+0

是的,這就是爲什麼我問,因爲有一個js captcha將是無用的,我想有一個php並將其包含在窗體中,並且處理我將使用'$ _POST'和Ajax執行此操作 – Roland 2012-01-13 11:41:07

+0

在此處如果您應該使用AJAX,建議兩次。您必須使用JS創建Ajax源代碼,並且在您的php文件中,您必須創建方法調用並簡單寫入響應。 – reporter 2012-01-13 11:50:02

回答

1

如果您想將變量從PHP傳遞給JS,您需要通過PHP生成JS代碼,或者您需要通過AJAX動態加載PHP結果並將其包含到您的表單中。

對於AJAX加載,使用這樣的:

$('#captcha_placeholder').load('/php_captcha.php'); 

如果您php_captcha.php文件應類似於你的,但應該呼應輸出。

您應該進行一些更改,因爲這僅僅是原始的想法。

+0

是的,我想將PHP中的變量傳遞給JS,我將如何使用Ajax將PHP加載到JS中? – Roland 2012-01-13 11:45:47

+0

編輯答案向您展示如何做到這一點的基本思路。 – 2012-01-13 12:12:41