2012-04-11 95 views
0

我的形式将验证在客户端,但我试图找出为什么它没有在服务器端验证。我没有完成我的PHP,但它甚至没有提出我的控制台中的POST请求,说它提交表单到服务器。不提交表单

的jQuery:

$(document).ready(function() 
{ 

/* 
* Validate the form when it is submitted 
*/ 
var validateform = $("#newArticleForm").validate({ 
    invalidHandler: function(form, validator) { 
     var errors = validator.numberOfInvalids(); 
     if (errors) { 
      var message = errors == 1 
      ? 'You missed 1 field. It has been highlighted.' 
      : 'You missed ' + errors + ' fields. They have been highlighted.'; 
      $('.box .content').removeAlertBoxes(); 
      $('.box .content').alertBox(message, {type: 'warning', icon: true, noMargin: false}); 
      $('.box .content .alert').css({ 
       width: '100%', 
       margin: '0', 
       borderLeft: 'none', 
       borderRight: 'none', 
       borderRadius: 0 
      }); 
     } else { 
      $('.box .content').removeAlertBoxes(); 
     } 
    }, 
    showErrors : function(errorMap, errorList) { 
     this.defaultShowErrors(); 
     var self = this; 
     $.each(errorList, function() { 
      var $input = $(this.element); 
      var $label = $input.parent().find('label.error').hide(); 
      $label.addClass('red'); 
      $label.css('width', ''); 
      $input.trigger('labeled'); 
      $label.fadeIn(); 
     }); 
    }, 
    submitHandler: function(form) { 
     var dataString = $('#newArticleForm').serialize(); 
     $.ajax({ 
      type: 'POST', 
      url: '/kowmanager/dashboard/articleSubmit', 
      data: dataString, 
      dataType: 'json', 
      success: function(data) { 
       if (data.error) { 
        $('.box .content').removeAlertBoxes(); 
        $('.box .content').alertBox(data.message, {type: 'warning', icon: true, noMargin: false}); 
        $('.box .content .alert').css({ 
         width: '', 
         margin: '0', 
         borderLeft: 'none', 
         borderRight: 'none', 
         borderRadius: 0 
        }); 
       } 
       else 
       { 
        $('.box .content').removeAlertBoxes(); 
        $('.box .content').alertBox(data.message, {type: 'success', icon: true, noMargin: false}); 
        $('.box .content .alert').css({ 
         width: '', 
         margin: '0', 
         borderLeft: 'none', 
         borderRight: 'none', 
         borderRadius: 0 
        }); 
        $(':input','#newArticleForm') 
        .not(':submit, :button, :hidden, :reset') 
        .val(''); 
       } 
      } 
     }); 
    } 
}); 

}); 

控制器:

function articleSubmit() 
{ 
    $outputArray = array('error' => 'yes', 'message' => 'unproccessed'); 
    $outputMsg = ''; 
    // Sets validation rules for the login form 
    $this->form_validation->set_rules('title', 'Title', 
     'trim|required|xss_clean|alpha_numeric'); 
    $this->form_validation->set_rules('category', 'Category', 
     'integer'); 
    $this->form_validation->set_rules('isSticky', 'Is Sticky', 
     'integer'); 
    $this->form_validation->set_rules('comments', 'Allow Comments', 
     'integer');  

    // Checks to see if login form was submitted properly 
    if (!$this->form_validation->run()) 
    { 
     $outputArray['message'] = 
      'There was a problem submitting the form! Please refresh the window and try again!'; 
    } 
    else 
    { 

    } 
} 

查看:

<?php $attributes = array('class' => 'validate', 'id' => 'newArticleForm'); ?> 
      <?php echo form_open_multipart('', $attributes) ?> 
       <div class="content no-padding"> 
        <div class="section _100"> 
         <?php echo form_label('Title', 'title'); ?> 

         <div> 
          <?php echo form_input('title', '', 'class="required"'); ?> 
         </div> 
        </div> 

        <div class="section _100"> 
         <?php echo form_label('Category', 'category'); ?> 

         <div> 
          <?php echo form_dropdown('category', $categories, '', 'class="required"'); ?> 
         </div> 
        </div> 

        <div class="section _100"> 
         <?php echo form_label('Is Sticky', 'sticky'); ?> 

         <div> 
          <?php 
                  $options = array(
                     '' => 'Please Select An Option', 
                     '0' => 'No', 
                     '1' => 'Yes', 
                    ); 
                  ?><?php echo form_dropdown('sticky', $options, '', 'class="required"'); ?> 
         </div> 
        </div> 

        <div class="section _100"> 
         <?php echo form_label('Allow Comments', 'comments'); ?> 

         <div> 
          <?php 
                  $options = array(
                     '' => 'Please Select An Option', 
                     '0' => 'No', 
                     '1' => 'Yes', 
                    ); 
                  ?><?php echo form_dropdown('comments', $options, '', 'class="required"'); ?> 
         </div> 
        </div> 

        <div class="section _100"> 
         <?php echo form_label('Date Comments Expire', 'datetime'); ?> 

         <div> 
          <input id="datetime" type="datetime" class="required" /> 
         </div> 
        </div> 

        <div class="section _100"> 
         <?php echo form_label('Status', 'status'); ?> 

         <div> 
          <?php 
                  $options = array(
                     '' => 'Please Select An Option', 
                     '0' => 'Inactive', 
                     '1' => 'Active', 
                    ); 
                  ?><?php echo form_dropdown('status', $options, '', 'class="required"'); ?> 
         </div> 
        </div> 

        <div class="section _100"> 
         <?php echo form_label('Image', 'file'); ?> 
         <div> 
          <?php echo form_upload('file', '', 'class="required"'); ?> 
         </div> 
        </div> 

        <div class="section _100"> 
         <?php echo form_label('Permalink', 'permalink'); ?> 

         <div> 
          <?php echo form_input('permalink', '', 'class="required"'); ?> 
         </div> 
        </div> 

        <div class="section _100"> 
         <?php echo form_label('Article', 'article'); ?><?php $attributes = array('name' => 'article', 'cols' => '30', 'rows' => '5', 'id' => 'article', 'class' => 'required') ?> 

         <div> 
          <?php echo form_textarea($attributes); ?> 
         </div> 
        </div> 
       </div><!-- End of .content --> 

       <div class="actions"> 
        <div class="actions-left"> 
         <?php echo form_reset(array('id' => 'reset', 'name' => 'reset'), 'Reset'); ?> 
        </div> 

        <div class="actions-right"> 
         <?php echo form_submit(array('id' => 'submit', 'name' => 'submit'), 'Submit'); ?> 
        </div> 
       </div><!-- End of .actions --> 
      <?php echo form_close(); ?> 

编辑:

我使用这个jQuery很好,但不知道是否其他形式任何人都有还有其他想法吗?

+0

你尝试加入'.complete()'和'.error()'处理程序'$ .ajax'请求?他们返回什么? – 2012-04-11 18:05:23

回答

0

我知道这个问题的答案的问题......它驱使我坚果所以我很乐意帮助你与这一个:))

笨的作品与POST请求的方式很特别。如果你这样做,而是使用GET请求,你会看到它的工作正常......所以发生了什么?

笨有CRSF令牌来确保您发布在一个安全的方式您的数据。 因此,请确保您将此crsf值与其余数据一起发送。

我给你举个例子,这是我的POST AJAX +笨的样子:

$.ajax({ 
     type: 'POST', 
     dataType: 'HTML', 
     data: { 
      somevalue : somevalue, 
      csrf_test_name : $.cookie('csrf_cookie_name') 
     }, 

...

正如你所看到的,你CRSF值存储在cookie中。我使用jquery插件cookie助手,但感觉炒使用任何其他插件。

然而,记住这个名字“csrf_test_name”总是预计笨使POST请求

有一个愉快的一天!


对于那些谁想要阅读更多一个回合这一点,这就是我所说的: http://aymsystems.com/ajax-csrf-protection-codeigniter-20

+0

奇怪,因为我有其他形式,我使用这项工作很好,我有上面的方式,但与这种形式它不适用于某种原因。 – 2012-04-11 18:13:26

+0

在你的配置文件中,你的global_xss_filtering是否设置为true? – 2012-04-11 18:14:36

+0

是的,它被设置为true – 2012-04-11 18:17:05