doutang1873 2018-07-05 13:55
浏览 43
已采纳

带有Recaptcha v2的PHP表单 - 表单刷新而不提交

I've read through many posts on here, and followed all the advice, but I'm unable to get this form to submit with Google Recaptcha v2. After pressing submit the form just refreshes.

The html form and the php are on the same page. I'm basically just taking a form that was already present and adding in the Google Recaptcha.

Any help would be much appreciated!

PHP:

<?php
 if(isset($_POST['submit']) && !empty($_POST['submit'])){ 
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){


        //your site secret key
        $secret = 'SECRET KEY';

        $gRecaptcha = $_POST['g-recaptcha-response'];

        $gRecaptcha = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];

        $response = file_get_contents($gRecaptcha);

        $responseData = json_decode($response);

        if($responseData->success){

          switch ($_POST['location']) {
            case 'Inverness':
              $Send = "email address";
              break;
            case 'Southlake':
              $Send = "email address";
              break;
        }

        $Subject = "A Message From your Website!";
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $location = $_POST['location'];
        $message = $_POST['message'];

        $mailbody="Name: $name

Email: $email

Phone: $phone

Location: $location

Message: $message

";
        mail($Send, $Subject, $mailbody);
        $succMsg = 'Your contact request have submitted successfully.';
            exit($succMsg); 
        } else {
            // if not show the error
            $errMsg = 'Robot verification failed, please try again.';
            echo $errMsg;
        }
      }else{
        // if recaptcha is not checked
        $errMsg = 'Please click on the reCAPTCHA box.';
    } 
}  
?> 

HTML:

<div id="contact-form" style="float:left;">
    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" enctype="text/plain">
        <table width="325" cellpadding="0" cellspacing="6">
            <tr>
                <td><label for="name">Name:</label></td>
                <td><input type="text" name="name" value=""/></td>
            </tr>
            <tr>
                <td><label for="email">Email:</label></td>
                <td><input type="text" name="email" id="email" value="" /></td>
            </tr>
            <tr>
                <td><label for="phone">Phone:</label></td>
                <td><input type="text" name="phone" id="phone"  value="" /></td>
            </tr>
            <tr>
                <td><label for="loc">Location:</label></td>
                <td><select name="location"><option value="Inverness">Inverness</option><option value="Southlake">Southlake</option></select></td>
            </tr>
            <tr>
                <td><label for="msg">Questions/Comments:</label></td>
                <td><textarea name="message" id="msg"  value="" cols="20" rows="5" /></textarea></td>
            </tr>
            <tr>
            <td>For spam prevention purposes, please answer the question below</td>
        </tr>
            <tr>

                <td><div style="width:200px" class="g-recaptcha" data-sitekey="SITE KEY"></div></td>
            </tr>
            <tr>
                <td colspan="2" align="left"><input type="submit" name='submit' value="Send" style="color:white;background-color:#bc2729;width:95px;height:29px;border:none" src="images/btn_submit.jpg"></td>
            </tr>
        </table>
    </form>


                </div>
  • 写回答

1条回答 默认 最新

  • dptpn06684 2018-07-06 19:01
    关注

    Remove the enctype="text/plain" attribute from your form and the form should submit. You can let it submit with the default enctype which is application/x-www-form-urlencoded.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)