Contact form with reCaptcha [closed]

我们两清 提交于 2019-12-12 04:16:17

问题


I know this may have been submitted before (sorry) I have basic form, these are the details id like to be sent, however i cannot get the reCaptcha to work with it. I have googled all day, but when i try other peoples code (amending to fit mine) it doesnt seem to work.

I would like: Name, Email, Number, newsletter (yes/no) and recaptcha to be sent/work.

Can someone please give me an idea where i may be going wrong? what i may need to add?

Thanks in advance!

Here is my Form (html)

<form method="POST" action="Form_Activation.php">
   <div class="form-group">
        <label for="name">Name:</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="Full Name"       value="" required/>
        </div>
        <div class="form-group">
            <label for="email">Email:</label>
            <input type="email" class="form-control" id="email" name="email" value=""       placeholder="you@example.com" required/>
        </div>
        <div class="form-group">
            <label for="number">Number:</label>
            <input class="form-control" name="number" id="number" value="" placeholder="Contact         Number" required/>
        </div>
        <div class="form-group">
            <label for="message">Message:</label>
            <textarea class="form-control" name="message" id="message" placeholder="Enter       Message.." required></textarea>
        </div>
        <div class="form-group">
            <input type="checkbox"/> <b> Subscribe to Newsletter</b>
        </div>
        <div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
        <button type="submit" class="btn btn-default sendbutton">SEND</button>
        </form>

Here is my php (basic)

    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $message = $_POST['message'];

    //$password = $_POST['password'];

    //$keyy = $_SERVER['UNIQUE_ID'];


    $msg = "Name: $name\r\n \r\n"; 

    $msg .= "Email: $email\r\n \r\n";

    $msg .= "Number: $number\r\n \r\n"; 

    $msg .= "Message: $message\r\n \r\n"; 

    $recipient = "info@islandwebdesign.co.uk";
    $subject = "New Website Request";
    $mailheaders = "From:$email";
    //$mailheaders .= "Reply-To:$email";
    mail($recipient,$subject,$msg,$mailheaders);
    header("Location: contactus.php?msg=1");
    ?>

回答1:


First of all make sure that you've included the necessary JavaScript resource to render reCAPTCHA widget properly, like this:

<html>
  <head>
    <title>reCAPTCHA demo: Simple page</title>
     <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  </head>
  <body>
    <form action="?" method="POST">
      <div class="g-recaptcha" data-sitekey="your_site_key"></div>
      <br/>
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

Here's the reference:

  • Displaying the widget

Now comes to your user's response. The response from the user's captcha challenge can be fetched in three ways. It can be as,

Now comes to your user's response. The response from the user's captcha challenge can be fetched in three ways. It can be as,

  • g-recaptcha-response - a POST parameter in the submitted form
  • grecaptcha.getResponse(widget_id) - will provide the response after the user completes the captcha.
  • A string argument to the callback function specified in the config object passed to the render method.

Here's the reference:

  • Verifying the user's response

For your purpose use g-recaptcha-response to get the user's response. So your code should be like this:

HTML

<form method="POST" action="Form_Activation.php">
   <div class="form-group">
    <label for="name">Name:</label>
        <input type="text" class="form-control" id="name" name="name" placeholder="Full Name" value="" required/>
    </div>
    <div class="form-group">
        <label for="email">Email:</label>
        <input type="email" class="form-control" id="email" name="email" value="" placeholder="you@example.com" required/>
    </div>
    <div class="form-group">
        <label for="number">Number:</label>
        <input class="form-control" name="number" id="number" value="" placeholder="Contact Number" required/>
    </div>
    <div class="form-group">
        <label for="message">Message:</label>
        <textarea class="form-control" name="message" id="message" placeholder="Enter Message.." required></textarea>
    </div>
    <div class="form-group">
        <input type="checkbox"/> <b> Subscribe to Newsletter</b>
    </div>
    <div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
    <button type="submit" name="submit" class="btn btn-default sendbutton">SEND</button>
</form>

Add a name attribute in your submit button.

Form_Activation.php

<?php

    if(isset($_POST['submit'])){

        //your site secret key
        $secret = 'XXXXXXX_Secret-key_XXXXXXX';

        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
            //get verified response data
            $param = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];
            $verifyResponse = file_get_contents($param);
            $responseData = json_decode($verifyResponse);

            if($responseData->success){
                // success

                $name = $_POST['name'];
                $email = $_POST['email'];
                $number = $_POST['number'];
                $message = $_POST['message'];

                // so on

            }else{
                // failure
            }

        }

    }

?>

Don't forget to add your secret key in $secret variable.



来源:https://stackoverflow.com/questions/34559780/contact-form-with-recaptcha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!