PHP& JavaScript |在JavaScript生成的表单中包含PHP Math Captcha

时间:2012-01-13 11:34:17

标签: php javascript captcha

首先,我使用以下代码通过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>标签的加载脚本。

1 个答案:

答案 0 :(得分:1)

如果要将变量从PHP传递给JS,则需要通过PHP生成JS代码,或者需要通过AJAX动态加载PHP结果并将其包含在表单中。

对于AJAX加载,请使用以下内容:

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

你的php_captcha.php文件应该与你的文件类似,但是应该回显输出。

你应该做一些改变,因为这只是一个原始想法。

相关问题