PHP&;JavaScript|在JavaScript生成的表单中包含PHP数学Captcha


PHP & JavaScript | Include PHP Math Captcha In JavaScript Generated Form

首先,我使用以下代码通过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);

我有以下代码来生成数学captcha:

<?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>标记的脚本

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

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

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

php_captcha.php文件的外观应该与您的文件相似,但应该回显输出。

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