dqp21271 2015-02-04 20:55
浏览 99

由AJAX打开的PHP文件 - 提供Captcha警报并发送url-data

Sitting on this already the whole day, if you believe or not :) I want to transfer data via GET-Method to an external .jsp file (example.com/data.php?attribute=value&a2=v2 and so on)

This all seems to work so far and also the validation. Now I have the problem that I have a captcha which is generated by a php-file and the captcha is controlled/validated in the PHP-File where the HTML-Form data is posted to.

When everything is OK I can do a alert in the submitHandler (success: function(response)) but how can I get an alert now, if the Captcha is incorrect?

How can I solve this? Can I get a value oder variable throught AJAX back from the PHP File to do something with it (to display the alert)?

Would be very glad for any help or comments.

Thank you!

HERE the data is validated and sent to PHP file after successful validation.

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">
(function($,W,D)
{
    var JQUERY4U = {};
    JQUERY4U.UTIL =
    {
        setupFormValidation: function()
        {
            $("#form-id").validate({
                rules: {
                    firstname: "required",
                    .........
                },
                messages: {
                    firstname: "Please enter your firstname",
                    ..........
                },
                submitHandler: function(form)
                    var data = $("#nachricht").serializeArray();
                    $.ajax({
                    type:"POST",
                    url:"nachricht.php",
                    data: data,
                    dataType: "text",
                    success: function(response) {
                    alert('Thank you!');
                        }
                    });
                }
            });
        }
    }
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
    });
})(jQuery, window, document);

THE PHP FILE: Here the data is transferred to another URL (GET-Method) and the Captcha is checked

$email = urlencode($_POST['email']);
$name = urlencode($_POST["name"]);
....

session_start();
if(isset($_SESSION['captcha_spam']) AND $_POST["captchafield"] == $_SESSION['captcha_spam']) // Captcha korrekt
{
    unset($_SESSION['captcha_spam']);

$link = "http://www.example.com/data.jsp?email=" . $email . "&name=" . $name . $titel . "&text=" . $text . "&ranking=" . $ranking . "&captchafield=" . $captcha_s;

header( "refresh:0;url=" . $link);
exit;
....
  • 写回答

1条回答 默认 最新

  • drblhw5731 2015-02-04 22:04
    关注

    OK so now I have in my PHP file where the form is submitted to:

    if(enteredcaptcha == captcha){
    echo "<script type='text/javascript'> alert('captcha_correct');</script>";  
    } else { 
    echo "<script type='text/javascript'> alert('captcha_not_correct');</script>";  
    }
    

    and in my normal HTML file where the Form and Javascript is I have:

    submitHandler: function(form) {
                        var data = $("#kommentar").serializeArray();
                        $.ajax({
                        type:"POST",
                        url:"kommentar.php",
                        data: data,
                        dataType: "text",
                        success: function(response) {
                        if( response == "captcha_is_correct" ){ alert("Success"); } else { alert("unsuccess.."); }
                            }
                        });
    
                    }
    

    And this makes really sense, wonderful, but I'm always getting the message "unsucces", so I think there is still a bug somehow?

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?