自动完成后,WordPress表单不会提交到数据库

时间:2018-09-24 00:42:47

标签: php sql wordpress forms

好吧,正如标题所述,如果它是自动完成的,它不会提交到数据库,但是如果尚未完成,它会提交给数据库。我不想禁用自动补全功能,而且我不确定为什么要这样做。我的MAMP文件夹中的PHP和MySQL的日志文件中也没有错误。最后,即使自动填充时它没有提交到数据库,它也确实重定向到了确认页面。

我的代码:

function request_private() {

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

    global $wpdb;

    $name = sanitize_text_field($_POST['privateName']);
    $phone = sanitize_text_field($_POST['privatePhone']);
    $email = sanitize_text_field($_POST['privateEmail']);
    $time = sanitize_text_field($_POST['privateTime']);
    $people = sanitize_text_field($_POST['privatePeople']);
    $type = sanitize_text_field($_POST['privateType']);
    $requests = sanitize_text_field($_POST['privateRequests']);
    $filter = sanitize_text_field($_POST['privateFilter']);
    $antiSpam = sanitize_text_field($_POST['privateAntiSpam']);

    if ($antiSpam === '8' && $name !== '' && $name !== ' ' && $phone !== '' && $phone !== ' ' && $time !== '' && $time !== ' ' && $people !== '' && $people !== ' ' && $type !== '' && $type !== ' ' && $filter === '') {

        $table = $wpdb->prefix . 'requests';

        $data = array(
            'name' => $name,
            'phone' => $phone,
            'email' => $email,
            'time' => $time,
            'people' => $people,
            'type' => $type,
            'requests' => $requests
        );

        $format = array(
            '%s',
            '%s',
            '%s',
            '%s',
            '%s',
            '%s',
            '%s'
        );

        $wpdb->insert($table, $data, $format);

        $url = get_page_by_title('Confirmation');

        wp_redirect( get_permalink($url) );

        $admin_email = get_option('admin_email');

        $admin_message = 'You have received a new private event from ' . $name . ' on/at ' . $time . '. More information can be found on WordPress under Forms > Private Events.';
        $customer_message = '' . $name . ', this is an email to confirm that we have received your request for a private event on ' . $time . '. We look forward to seeing you!';

        mail($admin_email, "Private Event Request Confirmation", $admin_message);
        mail($email, "Private Event Request Notification", $customer_message);

        exit();

    } else {
        return false;
    }

}

}

  add_action('init', 'request_private');

失败的HTTP POST请求:

请求网址:http://localhost:5757/private-events/?ckcachecontrol=1537755652

请求方法:POST

状态码:302找到

远程地址:[:: 1]:5757

推荐人政策:降级时不推荐人

接受范围:字节

缓存控制:max-age = 0,无缓存,无存储,必须重新验证

连接:保持活动

内容长度:0

Content-Type:文本/ html; charset = UTF-8

日期:2018年9月24日星期一02:20:59 GMT

保持活动:超时= 5,最大= 100

位置:http://localhost:5757/confirmation/

服务器:Apache / 2.2.34(Unix)mod_wsgi / 3.5 Python / 2.7.13 PHP / 7.2.1 mod_ssl / 2.2.34 OpenSSL / 1.0.2j DAV / 2 mod_fastcgi / 2.4.6 mod_perl / 2.0.9 Perl / v5.24.0

X-Powered-By:PHP / 7.2.1

接受:text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,image / apng, / ; q = 0.8

接受编码:gzip,deflate,br

Accept-Language:en-US,en; q = 0.9,la; q = 0.8,fr; q = 0.7

缓存控制:max-age = 0

连接:保持活动状态

内容长度:222

Content-Type:应用程序/ x-www-form-urlencoded

Cookie:wordpress_test_cookie = WP + Cookie + check; wordpress_logged_in_cd9b744c619529c4988e0e94344eaf12 = admin%7C1537891968%7CRSrR6dTZc4Vrj76lhdSRST31GpY77NksQJGVz2LcItd%7Ce1ce57f16867a7ab2c527d02f11bb3f4390341; wp-settings-1 = mfold%3Do%26libraryContent%3Dbrowse%26editor%3Dtinymce%26post_dfw%3Doff%26hidetb%3D0%26editor_expand%3Don; wp-settings-time-1 = 1537719169

主机:localhost:5757

来源:http://localhost:5757

引荐来源:http://localhost:5757/private-events/?ckcachecontrol=1537755652

不安全升级请求:1

用户代理:Mozilla / 5.0(Macintosh;英特尔Mac OS X 10_13_6)

AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 69.0.3497.100 Safari / 537.36

ckcachecontrol:1537755652

privateName:名称示例

私人电子邮件:email@email.com

私人电话:1111111111

privateTime:asd

privatePeople:asd

privateType:asd

privateRequests:asd

privateFilter:

私人反垃圾邮件:8

privateSubmit:提交请求

成功的HTTP POST请求:

请求网址:http://localhost:5757/private-events/

请求方法:POST

状态码:302找到

远程地址:[:: 1]:5757

推荐人政策:降级时不推荐人

接受范围:字节

缓存控制:max-age = 0,无缓存,无存储,必须重新验证

连接:保持活动

内容长度:0

Content-Type:文本/ html; charset = UTF-8

日期:2018年9月24日星期一02:43:14 GMT

保持活动:超时= 5,最大= 100

位置:http://localhost:5757/confirmation/

服务器:Apache / 2.2.34(Unix)mod_wsgi / 3.5 Python / 2.7.13 PHP / 7.2.1 mod_ssl / 2.2.34 OpenSSL / 1.0.2j DAV / 2 mod_fastcgi / 2.4.6 mod_perl / 2.0.9 Perl /v5.24.0

X-Powered-By:PHP / 7.2.1

接受:text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,image / apng, / ; q = 0.8

接受编码:gzip,deflate,br

Accept-Language:en-US,en; q = 0.9,la; q = 0.8,fr; q = 0.7

缓存控制:max-age = 0

连接:保持活动状态

内容长度:208

Content-Type:应用程序/ x-www-form-urlencoded

Cookie:wordpress_test_cookie = WP + Cookie + check; wordpress_logged_in_cd9b744c619529c4988e0e94344eaf12 = admin%7C1537891968%7CRSrR6dTZc4Vrj76lhdSRST31GpY77NksQJGVz2LcItd%7Ce1ce57f16867a7ab2c527d02f11bb3f4390341; wp-settings-1 = mfold%3Do%26libraryContent%3Dbrowse%26editor%3Dtinymce%26post_dfw%3Doff%26hidetb%3D0%26editor_expand%3Don; wp-settings-time-1 = 1537719169

主机:localhost:5757

来源:http://localhost:5757

引荐来源:http://localhost:5757/private-events/

不安全升级请求:1

用户代理:Mozilla / 5.0(Macintosh;英特尔Mac OS X 10_13_6) AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 69.0.3497.100 Safari / 537.36

privateName:名称示例

私人电子邮件:email@email.com

私人电话:1111111111

privateTime:asd

privatePeople:asd

privateType:asd

privateRequests:

privateFilter:

私人反垃圾邮件:8

privateSubmit:提交请求

1 个答案:

答案 0 :(得分:0)

使用自动完成或手动填写表格都无关紧要。如果生成的HTTP请求是相同的,并且以相同的状态发送到服务器,则对请求的处理应相同。我只看到三种可能性。

  1. HTTP请求不相同。
  2. HTTP服务器的状态不同。
  3. 测试程序在某种程度上是不正确的。

我区分了成功和失败的HTTP请求,但有一些区别:

  1. 失败的HTTP请求具有附加的ckcachecontrol查询参数和ckcachecontrol POST参数。
  2. POST参数privateRequests具有不同的值。
  3. 日期,内容长度和引荐来源网址HTTP字段不同。

要尝试的第一个实验是从失败的请求中删除ckcachecontrol查询参数和ckcachecontrol POST参数。 (是否有可能使ckcachecontrol查询参数使服务器缓存绕过PHP处理而将HTTP重定向返回到“ http://localhost:5757/confirmation/”?)

如果这不能解决问题,则我需要进行更多实验。必须有一个解释。