0

您好,我有一个使用 ajax 的 recaptcha,但它没有将我的表单信息插入数据库,它插入空行,我不知道为什么这是我的代码:

 require_once('inc/recaptchalib.php');
$publickey = "6LdW1N0SAAAAADMIPPkOGd939meXV9a9qDwwcxbu"; // you got this from the signup page
$privatekey = "";


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"     type="text/javascript"></script>
 <script type="text/javascript">
 var RecaptchaOptions = {
    theme : 'clean'
 };
 </script>
<script type="text/javascript">
function validateCaptcha()
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
    //alert(challengeField);
    //alert(responseField);
    //return false;
    var html = $.ajax({
    type: "POST",
    url: "ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;

    if(html == "success")
    {
        $("#captchaStatus").html("Success. Submitting form.");
        return false;
        // Uncomment the following line in your application
        //return true;
    }
    else
    {
        $("#captchaStatus").html("Your captcha is incorrect. Please try again");
        Recaptcha.reload();
        return false;
    }
}
</script>

这是我要插入数据库的表格:

<form id="loginform" name="loginform" action="" method="post" enctype="multipart/form-data" onSubmit="return validateCaptcha()" >
                        <fieldset class="step">

                            <p>
                                <label for="nick">Nickname:</label>
                                <input type="text" name="nick" value="" id="nicknamee" value="" >
                            </p>
                            <p>
                                <label for="email">Email:</label>
                                <input type="text" name="email" value="" id="email_comm" value="" >
                            </p>

                            <p>  
                                <textarea class="text-input textarea wysiwyg" name="add_comment" id="text_commentt"></textarea>
                            </p>';?>
                            <div id="login">

    <?php echo recaptcha_get_html($publickey);?>

<p style="color: red;" id="captchaStatus">&nbsp;</p>

</div>

<?php
    echo'                        
                            <p style="margin-top:200px;"><button id="vote_petiton_submit" type="submit" ></button>
                            </p>

                        </fieldset>
                        </form>

现在验证 reCaptcha 和数据库插入文件。验证没问题,只是在数据库中插入数据不能正常工作,它不会发布值。

<?php
require_once('inc/recaptchalib.php');
require_once('engine/db.php');
$publickey = "6LdW1N0SAAAAADMIPPkOGd939meXV9a9qDwwcxbu"; // you got this from the signup page
$privatekey = "";
 mysql_query("SET NAMES utf8");
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if ($resp->is_valid) {
    ?>success<?
    $nick=$_REQUEST['nick'];
        $type=0;   
            $email=$_REQUEST['email'];
            $descr=$_REQUEST['add_comment'];
            $descriere=strip_tags($descr,'<br><p><a><i><u><b>');    
            $id=150;

            $insert = "INSERT INTO comments (`nickname`,`desc`,`email`,`type`,`id_pet_dem_des`)
                        VALUES ('$nick','$descriere','$email','$type',$id)";
                $result = mysql_query($insert)
                    or die("query failed: " . mysql_error());


          $queryz = "update petition set comments=comments+1 where id_petition='$id'";
    $resultz = mysql_query($queryz)
        or die("query failed: " . mysql_error());
}
else 
{
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}
?>

提前谢谢。

4

1 回答 1

1

我通过更改 ajax 帖子中的数据来修复它

data: $('#loginform').serialize() + "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField, 
于 2013-03-06T09:49:58.390 回答