0

Sorry for my bad english. I'm trying to run a PHP function through an ajax script. The PHP script should run as a FULL NORMAL php script. My idea is to run a recaptcha by a Submit button WITHOUT refreshing the page. That is working, but I found no way to run a normal php script after that. Here is the part of my script.

php:

if( isset( $_REQUEST['startattack'] )){
    $secret="********************";
    $response=$_POST["g-recaptcha-response"];
    $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");

    $captcha_success=json_decode($verify);   

    if ($captcha_success->success==false) {     
        echo "<script type='text/javascript'>alert('failed!')</script>";
    } else if ($captcha_success->success==true) {
        echo "<script type='text/javascript'>alert('success!')</script>"; 
    }       
}

html:

<form method='post' id="myform">
    <center>
        <div class="g-recaptcha" data-sitekey="6LfETygTAAAAAMC7bQu5A3ZhlPv2KBrh8zIo_Nwa"></div>
    </center>
    <button type="submit" id="startattack" name="startattack" onclick="mycall()" class="btn btn-attack">Start Attack</button>
</form>

ajax:

<script>
    $(function () {
        $('button').bind('click', function (event) {
            $.ajax({
                type: 'POST',
                url: 'post.php',
                data: $('button').serialize(),
                success: function () {
                    alert('button was submitted');
                    type: 'post';
                    url: 'post.php';
                }
            });

            event.preventDefault();// using this page stop being refreshing 
        });
    });
</script>

I want to check the recaptcha here. If correct, it should echo correct in PHP and I want to add feature later. The same with the false captcha.

1 Answer 1

1

I think you can simplify things a bit. You don't return the response in the Ajax is your main problem.

PHP:

Just echo the returned json from the recaptcha (although I have no idea where you get the g-recaptcha-response key/value, you are not sending it anywhere).

if(isset( $_POST['startattack'] )){
    $secret   = "********************";
    // I have added a key/value in the ajax called "sitekey",
    // this might be what you are trying to retrieve?
    $response = $_POST["g-recaptcha-response"];
    echo file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
    exit;
}

AJAX:

I think since the return from the recaptcha is json anyway, just echo it and pick it up on this side:

$(function () {
    $('button').bind('click', function (event) {
        var statusBlock =   $('#status');
        statusBlock.text('button was submitted');
        $.ajax({
            type: 'POST',
            url: 'post.php',
            data: {
                // Not sure if you are trying to pass this key or not...
                "sitekey":$('.g-recaptcha').data('sitekey'),
                "startattack":true
            },
            success: function (response) {
                var decode    = JSON.parse(response);
                var alertMsg  = (decode.success)? 'Success' : 'Failed';
                statusBlock.text('');
                alert(alertMsg);
            }
        });
        // using this page stop being refreshing 
        event.preventDefault();
    });
});

Form:

Leave a spot to post the submit status so it doesn't interfere with the return alert dialog window.

<form method='post' id="myform">
    <div id="status"></div>
    <center>
        <div class="g-recaptcha" data-sitekey="6LfETygTAAAAAMC7bQu5A3ZhlPv2KBrh8zIo_Nwa"></div>
    </center>
    <button type="submit" id="startattack" name="startattack" onclick="mycall()" class="btn btn-attack">Start Attack</button>
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.