首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有Recaptcha v2的PHP表单-表单无需提交即可刷新

带有Recaptcha v2的PHP表单-表单无需提交即可刷新
EN

Stack Overflow用户
提问于 2018-07-05 21:55:20
回答 1查看 142关注 0票数 0

我已经阅读了这里的许多帖子,并遵循了所有的建议,但我无法让这个表格提交给谷歌Recaptcha v2。按下submit后,表单就会刷新。

html表单和php在同一个页面上。我基本上只是采用一个已经存在的表单,并添加到Google Recaptcha中。

任何帮助都将不胜感激!

PHP:

代码语言:javascript
复制
<?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\n\nEmail: $email\n\nPhone: $phone\n\nLocation: $location\n\nMessage: $message\n\n";
        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:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-07 03:01:47

从表单中删除enctype="text/plain"属性,表单就应该提交了。您可以让它使用默认的加密类型application/x-www-form-urlencoded提交。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51193170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档