前端发布表单验证

时间:2012-09-20 作者:Pollux Khafra

我有一个前端帖子,除了我需要在处理之前进行的验证外,它工作得很好。最终,我希望能够用ajax做到这一点,因为我必须用php函数验证youtube url,但我想从显示空标题等错误开始。这是我到目前为止的情况。

  <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">

    <p>
      <input type="text" id="title" value="Video Title" tabindex="1" size="20" name="title" />
    </p>

    <p>
      <textarea id="description" tabindex="3" name="description" cols="50" rows="6">Video Description</textarea>
    </p>

    <p>
      <div class="icon video"></div>
      <input type="text" id="video_url" value="Youtube URL" tabindex="1" size="20" name="video_url" />
      <div class="video-validation"></div>
    </p>

    <p>
      <input type="text" value="Tags" tabindex="5" size="16" name="post_tags" id="post_tags" />
    </p>

    <p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

    <input type="hidden" name="action" value="new_post" />

    <?php wp_nonce_field( \'new-post\' ); ?>

  </form>
然后是要在这里处理的PHP,它似乎会检查字段是否已设置,但如果未设置,则不会执行任何操作。

function video_process_form( $query ) {
 if ( $query->is_page( \'submit-video\' ) && isset( $_POST[\'title\'] ) ) {
   if ( ! function_exists( \'wp_handle_upload\' )) {
            require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
   }
   $file=$_FILES;
   // Do some minor form validation to make sure there is content
   if ( isset($_POST[\'title\']) && !empty($_POST[\'title\'])) {
    $title =  $_POST[\'title\'];
   } else {
    echo \'Please enter a game  title\';
   }
   if (isset ($_POST[\'description\'])) {
    $description = $_POST[\'description\'];
   } else {
    echo \'Please enter the content\';
   }
   if (isset ($_POST[\'video_url\'])) {
    $video_url = $_POST[\'video_url\'];
   }
   $tags = $_POST[\'post_tags\'];

   // Add the content of the form to $post as an array
   $new_post = array(
    \'post_title\'    => $title,
    \'post_content\'  => $description,
    \'tags_input\'    => array($tags),
    \'post_category\' => array(12),
    \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
    \'post_type\' => fod_videos  // Use a custom post type if you want to
   );
   //save the new post and return its ID
   if (isset($_POST[\'title\']) && !empty($_POST[\'title\'])) {
    $pid = wp_insert_post($new_post);
    update_post_meta($pid,\'video_code\',$video_url);
    wp_redirect( get_permalink($pid)); 
    exit();
   }
 }
 do_action(\'wp_insert_post\', \'wp_insert_post\');
}
add_action( \'pre_get_posts\', \'video_process_form\' );    
那么,我在这里是如何失败的,提交时向客户显示错误的正确方法是什么?也不,我不想用重力的形式来解决我所有的问题。

1 个回复
最合适的回答,由SO网友:Dave Hunt 整理而成

使用isset并不是最好的选择,因为当您提交表单时,post变量仍然会被设置为null。您应该检查这些值是否为null。此外,当您应该将验证错误消息分配给一个变量并返回该变量,然后在模板中输出返回值时,您也在回显验证错误消息。

function video_process_form( $query ) {
    global $error_output; 
    if ( $query->is_page( \'submit-video\' ) && isset( $_POST[\'title\'] ) ) {

        if ( ! function_exists( \'wp_handle_upload\' )) {
            require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
        }

        $file = $_FILES;

        // Do some minor form validation to make sure there is content
        if ($_POST[\'title\'] != null) {
            $title =  $_POST[\'title\'];
        } else {
            $error_output .= \'Please enter a game  title<br/>\';
        }

        if ($_POST[\'description\'] != null) {
            $description = $_POST[\'description\'];
        } else {
            $error_output .= \'Please enter the content<br/>\';
        }

        if ($_POST[\'video_url\'] != null) {
            $video_url = $_POST[\'video_url\'];
        }
        $tags = $_POST[\'post_tags\'];

        // Add the content of the form to $post as an array
        $new_post = array(
            \'post_title\'    => $title,
            \'post_content\'  => $description,
            \'tags_input\'    => array($tags),
            \'post_category\' => array(12),
            \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
            \'post_type\' => fod_videos  // Use a custom post type if you want to
        );

        // If No Errors, Save Post and Redirect //
        if ($error_output == null) {
            $pid = wp_insert_post($new_post);
            update_post_meta($pid,\'video_code\',$video_url);
            wp_redirect( get_permalink($pid)); 
        // If Errors, Return Errors for Display in Template //
        } 
    }
    do_action(\'wp_insert_post\', \'wp_insert_post\');
}
add_action( \'pre_get_posts\', \'video_process_form\' );  
然后在模板文件中,您可以执行以下操作:

global $error_output;
if ($error_output != null) {
    echo \'<div class="errorbox">\' . $error_output . \'</div>\';
} 

结束

相关推荐

jQuery AJAX form validation

我正在尝试为我的站点创建一个自定义登录页面,即http://localhost/site/login, 我现在遇到的问题是,当用户尝试登录并输入错误的用户名或密码时,他们会被重定向到wp-login.php 您可以想象,这是一个有问题的页面,因此我想将AJAX表单验证添加到我的自定义表单中,但问题是我不确定如何以最佳方式进行验证。如何使用jQuery AJAX验证用户?这就是我迄今为止所做的:jQuery:$(\'#selector\').live(\'click\', function(){