I have a working form with captcha. When I load up the webpage and click on submit without checking the box , the alert shows (click on robot), but then if I let the captcha expire and click submit again, the alert doesn't pop up any more. Is there something wrong in the following code or any way to rewrite it?
<script>
    window.onload = function() {
    var recaptcha = document.forms["form"]["g-recaptcha-response"];
    recaptcha.required = true;
    recaptcha.oninvalid = function(e) {     
        alert("Please click on i am not a robot");
        }
    }
</script>
<script src="https://www.google.com/recaptcha/api.js"></script>
<div class="g-recaptcha" data-sitekey="xxx" data-callback="recaptcha"></div>
Please take a look at my server side php. I tried to add data expired callback several times in past few days but couldn't make it work.
$recaptcha_secret = "xxx";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
        $response = json_decode($response, true);
        if($response["success"] === true) {
                mail($to, $subject, $body, "From:" . $email);
        }
        else
        {
        return false;
        }
        echo 'success';
    }