【问题标题】:wordpress - frontend post form issuewordpress - 前端帖子表单问题
【发布时间】:2015-09-21 04:17:25
【问题描述】:

我在前端有一个发布表单,用户可以从该表单发布 (post_type = product)。作为其中的一部分,我尝试实现一些服务器端验证,如下面的代码所示。问题是验证一切正常,但即使验证失败,数据也会保存在表单提交中。 理想情况下,当字段验证失败时,表单提交应该会失败。 我不确定$hasError = true 是否正常工作,我可能缺少一个非常简单的逻辑,但我没有得到。对此有任何帮助吗?

提前致谢。

$postTitleError = '';

    if (isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

        if (trim($_POST['postTitle']) === '') {
            $postTitleError = 'msg 1';
            $hasError = true;
        }
        if (trim($_POST['postCat1']) === '') {
            $postTitleError = 'msg2';
            $hasError = true;
        }
        if (trim($_POST['postPrice']) === '') {
            $postTitleError = 'msg3';
            $hasError = true;
        }
        if (trim($_POST['postTime']) === '') {
            $postTitleError = 'msg4';
            $hasError = true;
        }
        if (trim($_POST['postTimeMin']) === '') {
            $postTitleError = 'msg5';
            $hasError = true;
        }
        if (trim($_POST['postContent']) === '') {
            $postTitleError = 'msg6';
            $hasError = true;
        }

【问题讨论】:

    标签: php wordpress forms validation


    【解决方案1】:
    <?php
            //$postTitleError = '';
            $resultArr = array();
            $error_msg = false;
    
        if (isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
    
            if (isset($_POST['postTitle'])  && !empty($_POST["postTitle"])) {
                //$postTitleError = 'msg 1';
                //$hasError = true;
                $postTitle=$_POST['postTitle'];
    
            }
            else
            {
                $resultArr['status'] = 'failure';
                $resultArr['error_msg_postTitle']= "msg 1";
                $error_msg = true;
            }
    
            if (isset($_POST['postCat1']) && !empty($_POST["postCat1"]) ) {
               // $postTitleError = 'msg2';
               // $hasError = true;
               $postCat1=$_POST['postCat1'];
            }
            else
            {
                $resultArr['status'] = 'failure';
                $resultArr['error_msg_postCat1']= "msg2";
                $error_msg = true;
            }
    
    
            if (isset($_POST['postPrice']) && !empty($_POST["postPrice"]) ) {
               // $postTitleError = 'msg3';
                //$hasError = true;
                $postPrice=$_POST['postPrice'];
            }
            else
            {
                $resultArr['status'] = 'failure';
                $resultArr['error_msg_postPrice']= "msg3";
                $error_msg = true;
            }
    
    
            if (isset($_POST['postTime']) && !empty($_POST["postTime"]) ) {
                //$postTitleError = 'msg4';
                //$hasError = true;
                $postTime=$_POST['postTime'];
            }
            else
            {
                $resultArr['status'] = 'failure';
                $resultArr['error_msg_postTime']= "msg4";
                $error_msg = true;
            }
    
    
            if (isset($_POST['postTimeMin']) && !empty($_POST["postTimeMin"]) ) {
               // $postTitleError = 'msg5';
               // $hasError = true;
               $postTimeMin=$_POST['postTimeMin'];
            }
            else
            {
                $resultArr['status'] = 'failure';
                $resultArr['error_msg_postTimeMin']= "msg5";
                $error_msg = true;
            }
    
    
            if (isset($_POST['postContent']) && !empty($_POST["postContent"]) ) {
                //$postTitleError = 'msg6';
               // $hasError = true;
               $postContent=$_POST['postContent'];
            }
            else
            {
                $resultArr['status'] = 'failure';
                $resultArr['error_msg_postContent']= "msg6";
                $error_msg = true;
            }
    
            if($error_msg == false)
            {
                //here publish post code 
            }
            else
            {
    
                //here Error message prine
            }
    ?>
    

    【讨论】:

    • 感谢 Maulik 帕特尔。我对代码做了一些改动并实现了,它就像魅力一样工作,我明白错误在哪里。
    猜你喜欢
    • 1970-01-01
    • 2012-07-03
    • 2015-09-20
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多