Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jsanwo64's avatar

CAPTHCA with laravel

i keep getting error with the following


// Checks if form has been submitted
@if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }

    // Call the function post_captcha
    $res = post_captcha($_POST['g-recaptcha-response']);

    @if (!$res['success']) {
        // What happens when the CAPTCHA wasn't checked
        echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
    } else {
        // If CAPTCHA is successfully completed...

        // Paste mail function or whatever else you want to happen here!
        echo '<br><p>CAPTCHA was completed successfully!</p><br>';
    }
} else { 
                        <!-- Default Form -->
                        <div class="default-form contact-form">
                            <form method="post" action="{{ url('/contact') }}" id="contact-form" role="form">
                                @csrf
                                <div class="form-group{{ $errors->has('fullname') ? ' has-error' : '' }}">
                                    <input type="text" name="fullname" value="{{ old('fullname') }}" placeholder="Name*" required class="form-control">
                                     @if ($errors->has('fullname'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('fullname') }}</strong>
                                    </span>
                                @endif
                                </div>
                                
                                <div class="form-group{{ $errors->has('phone_number') ? ' has-error' : '' }}">
                                    <input type="number" name="phone_number" value="{{ old('phone_number') }}" placeholder="Phone Number*" required class="form-control">
                                     @if ($errors->has('phone_number'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('phone_number') }}</strong>
                                    </span>
                                @endif
                                </div>
                                
                                <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                    <input type="email" name="email" value="{{ old('email') }}" placeholder="Email*" required class="form-control">
                                     @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                                </div>
                                
                                <div class="form-group{{ $errors->has('message') ? ' has-error' : '' }}">
                                    <textarea name="message" placeholder="Your Message" class="form-control"></textarea>
                                     @if ($errors->has('message'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('message') }}</strong>
                                    </span>
                                @endif
                                </div>
                                
                                <div class="form-group">
                                    <button type="submit" class="theme-btn btn-style-one"><span class="txt">Submit now</span></button>
                                </div> 
                                
                                <div class="g-recaptcha" data-sitekey=""></div>
                                
                            </form> 
                            
                            }@endif

0 likes
3 replies
bobbybouwmann's avatar

What is the error? What exactly is not working?

Also you can't mix @if (condition) with } else {. Instead you need to use @if (condition) and @else

1 like
Snapey's avatar

You are also mixing php in with the blade view without @php declaration

Please or to participate in this conversation.