PHP codeigniter表单验证失败。流程的错误是什么?

时间:2017-10-11 11:00:25

标签: php validation codeigniter-2

这是我的表单视图

<?php echo form_open('', 'id="merchantReg"'); ?>
<div class="modalFormBody">
    <div class="modalFormRow">
        <div class="form-group modalFormGroup modalFormGroup1">
            <label for="fname">First name</label>
            <input type="text" class="form-control modalForm" name="mr_f" id="mr_fname" placeholder="Your first name">
            <p id="fnameValidation"></p>
        </div>
        <div class="form-group modalFormGroup modalFormGroup2">
            <label for="lname">Last name</label>
            <input type="text" class="form-control modalForm" name="mr_lname" id="mr_lname" placeholder="Your last name">
            <p id="lnameValidation"></p>
        </div>
    </div>
    <div class="modalFormRow">
        <div class="form-group modalFormGroupBlock">
            <label for="fname">Business name</label>
            <input type="text" class="form-control modalForm" name="mr_bname" id="mr_bname" placeholder="Your business name">
            <p id="bnameValidation"></p>
        </div>
    </div>
</div>
<?php echo form_close(); ?>

这是我的js

<script>
    if (vFN && vLN && vCon && emailValidation && Vbn) {
        createXMLHttpRequests();
        var url = baseURL + "admin/getting_started/creatMerchantRequest";
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("POST", url, true);
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                var res = xmlHttp.responseText;
                obj = JSON.parse(res);
                if (obj) {
                    document.getElementById('homeMerchantRes').style.display = 'block';
                } else {

                }
            }
        };
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        var vars = "fn=" + fn + "&ln=" + ln + '&e=' + e + '&cont=' + cont + '&bn=' + bn;
        xmlHttp.send(vars);
    }
</script>

这是我的控制器功能

<?php
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');

$mid = 0;
// $this->form_validation->set_rules('mr_f', 'First Name', 'trim|required|min_length[2]|max_length[38]|xss_clean');
$this->form_validation->set_rules('mr_f', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
$this->form_validation->set_rules('mr_lname', 'Last Name', 'trim|required|min_length[2]|max_length[38]|xss_clean');
$this->form_validation->set_rules('mr_bname', 'Business Name', 'trim|required|min_length[2]|max_length[38]|xss_clean');


if ($this->form_validation->run() == TRUE) {

} else {
    $mid = $this->form_validation->error_array();
    print_r($mid);
}
?>

php codeigniter验证规则不起作用。可以任何人解释是什么原因。我

1 个答案:

答案 0 :(得分:0)

1)获取表单值并传递给表单

<script type="text/javascript"> 
  $(document).ready(function(){
    var dataString = $("#FormId").serialize();
    var url="ControllerName/MethodName"
        $.ajax({
        type:"POST",
        url:"<?php echo base_url() ?>"+url,
        data:dataString,
        success:function (data) {
            alert(data);
        }
        });     
  })
</script>

在构造中加载库form_validation为...

$this->load->library('form_validation');

$this->load->helper('form');

现在将您的控制器编写为..

//您的控制器代码。

$mid = 0;
   // $this->form_validation->set_rules('mr_f', 'First Name', 'trim|required|min_length[2]|max_length[38]|xss_clean');
    $this->form_validation->set_rules('mr_f', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
    $this->form_validation->set_rules('mr_lname', 'Last Name', 'trim|required|min_length[2]|max_length[38]|xss_clean');
    $this->form_validation->set_rules('mr_bname', 'Business Name', 'trim|required|min_length[2]|max_length[38]|xss_clean');


    if($this->form_validation->run() == TRUE) {

       }
       else{
        $mid = $this->form_validation->error_array();
        print_r($mid);
      }`