简体   繁体   中英

wordpress - frontend post form issue

I have a post form on front end where users can post ( post_type = product ) from the form. As a part of it I have tried implementing few server side validations as in below code. The issue is that the validations are all working fine but the data is getting saved on form submission even when the validation fails. Ideally the form submission should fail when there is a field validation failure. I am not sure if $hasError = true is working or not, there might be a very simple logic I am missing which I am not getting. Any help regarding this?

Thanks in advance.

$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
        //$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
        }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM