当ajax用于表单提交时,在codeigniter中进行表单验证

时间:2016-11-03 15:21:20

标签: ajax codeigniter validation

此方案中未发生表单提交..

$.ajax({
   type: "POST", 
   async: false, 
   url: base_url+"register/registration_val",   
   data: "register_first_name="+first_name,
   success: function(data){
       $('#inferiz').html(data);
   },
   error: function(){
       alert('error');
   }

4 个答案:

答案 0 :(得分:0)

在您的视图中,您可以添加以下内容:

<script type="text/javascript">
  var base_url = "<?php print base_url(); ?>";
</script>

另外尝试提醒并查看ajax中最终网址的值,即alert(网址);

答案 1 :(得分:0)

尝试在名字输入

中添加ID
<script type="text/javascript">
$(document).on('submit','#form-reg',function(){ // #form-reg is id on form open tag
$.ajax({
  url: "<?php echo base_url('register/registration_val');?>",
  type: 'POST',
  data: {
    firstname: $('#firstname').val(),
  },
  dataType: 'html', // I perfer to use json
  success: function(data){
    $('#inferiz').html(data);
  },
  error: function(){
    alert('error');
  }
  }
});
});
</script>

我会使用dataType:json更容易从控制器

获取数据

答案 2 :(得分:0)

您使用data: "register_first_name="+first_name,它不正确。更正为data: {register_first_name:first_name},

base_url喜欢这个var base_url = <?php echo base_url(); ?>

所以,Bellow最终代码:

<script type="text/javascript">
jQuery(document).ready(function ($) {   
    var base_url = <?php echo base_url(); ?>

    $.ajax({
        url: base_url+"register/registration_val", // Url to which the request is send
        type: "POST", // Type of request to be send, called as method
        data: {register_first_name:first_name}, // Data sent to server, a set of key/value pairs representing form fields and values
        contentType: false, // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
    }).done(function (data) {
        $('#inferiz').html(data);           
    }).fail(function (data) {
        console.log('failed');
    });
}(jQuery));
</script>

答案 3 :(得分:0)

请验证您的查看部分是否提供了与ajax功能相同的ID。

查看部分:

<form  id="form-reg">
<input name="firstname" id="firstname" type="text" required placeholder="Enter firstname " >
<span id="name_validation" class="text-danger"></span>
<button name="submit"  id="submit_button"  onClick="myFunction();" >submit</button>
</form>

然后纠正必须在php标记内给出的基本URL路径。

function myFunction() { 
   $.ajax({
        url: "<?php echo base_url();?>register/registration_val",
        type: "POST",
        data:'firstname='+$("#firstname").val(),

        success: function(msg)
        {
            alert('done..!');
        }               
    });
}