简体   繁体   中英

PHP codeigniter form validation fails. What is the error of the flow

this is my form view

<?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(); ?>

this is my 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>

this is my controller function

<?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);
}
?>

the php codeigniter validation rules are not working. Can Any one explain what is the reason for that. I

1) Get get form value and pass to form as

<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>

Load library form_validation in construct as ...

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

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

Now write your controller as ..

// your controller code.

$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);
      }`

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