0

我有一个表单,允许我将新客户添加到数据库表中。

customer_name 字段是唯一字段。因此,我想在尝试写入数据库之前对输入字段执行表单验证。

理想情况下,验证可以是双重的。当用户输入客户名称时,显示名称是否可用的 ajax 查找。其次,codeigniter的验证功能?如果这不可用,也许在提交时可以进行查询以查看是否存在然后进行相应处理?

我已经审查并广泛搜索了一个完整的示例,但没有遇到任何示例。试了几次都没有运气。不幸的是,我的 java 脚本和 jquery 技能不存在,并试图在代码点火器周围感受我的方式。

我已经查看并尝试了以下文章: http: //vortexdev.netii.net/article_17/Check_the_user_name_availability_with___Codeigniter_jQuery CodeIgniter - 检查数据库中是否已存在值 http://www.joshuawinn.com/check-if-email -username-exists-with-codeigniter-and-jquery-validation/

我目前的代码是:

jQuery链接:

<head>
    <meta charset="utf-8">
    <title><?php  echo $title; ?></title>
    <link href="<?php echo base_url();  ?>styles/style.css" type="text/css" rel="stylesheet">
    <link href="<?php echo base_url();  ?>styles/menu.css" type="text/css" rel="stylesheet">
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script>  
    <script type="text/javascript">

    </script>   
</head>

我的控制器:

function create_customer()
    {

        $this->form_validation->set_rules("customer_name","`Customer Name`","required|min_length[6]|xss_clean");
        $this->form_validation->set_rules("address_line_1","`Address Line 1`","required|xss_clean|min_length[6]");
        $this->form_validation->set_rules("address_line_2","`Address Line 2`","xss_clean|min_length[6]");
        $this->form_validation->set_rules("suburb","`Suburb`","required|xss_clean|min_length[6]");
        $this->form_validation->set_rules("city","`City`","required|xss_clean|min_length[6]");
        $this->form_validation->set_rules("postalcode","`Postal Code`","required|xss_clean|min_length[4]|max_length[5]");
        $this->form_validation->set_rules("primary_contact_name","`Contact Person Name`","required|xss_clean|min_length[6]");
        $this->form_validation->set_rules("primary_contact_email","`Contact Person email`","required|valid_email|xss_clean");
        $this->form_validation->set_rules("primary_contact_tell","`Contact Person tell`","required|xss_clean|min_length[10]|max_length[14]");

        if ($this->form_validation->run() == FALSE){
            $data["message"]="";

            $data['title']="Master Data Home Page";
            $this->load->view("master_data/view_master_data_header",$data);
            $this->load->view("master_data/view_master_data_nav");
            $this->load->view("master_data/view_content_master_data_create_customer");
            $this->load->view("master_data/view_master_data_footer");
        } else {

        $data = array(
            'customer_name' =>  $this->input->post('customer_name'),
            'address_line_1' =>  $this->input->post('address_line_1'),
            'address_line_2' =>  $this->input->post('address_line_2'),
            'suburb' =>  $this->input->post('suburb'),
            'city' =>  $this->input->post('city'),
            'postalcode' =>  $this->input->post('postalcode'),
            'primary_contact_name' =>  $this->input->post('primary_contact_name'),
            'primary_contact_email' =>  $this->input->post('primary_contact_email'),
            'primary_contact_tell' =>  $this->input->post('primary_contact_tell'),
            );

        $this->model_master_data->add_record($data);

        $this->customer_created_successfully();
            }
        }

和我的观点:

<?php
echo validation_errors();
?>
<br>
<?php 
    echo form_open('masterdata/create_customer');
?>

<table>
    <tr>
        <td valign="top">

<?php   

    echo form_label("Customer Name", "customer_name");
    $data= array(
    "name"=>"customer_name",
    "id"=>"customer_name",
    "value"=>set_value("customer_name"));
    echo form_input($data);

    echo form_label("Contact Name", "primary_contact_name");
    $data= array(
    "name"=>"primary_contact_name",
    "id"=>"primary_contact_name",
    "value"=>set_value("primary_contact_name"));
    echo form_input($data);

        echo form_label("Contact Email", "primary_contact_email");
    $data= array(
    "name"=>"primary_contact_email",
    "id"=>"primary_contact_email",
    "value"=>set_value("primary_contact_email"));
    echo form_input($data);


    echo form_label("Contact Tel", "primary_contact_tell");
    $data= array(
    "name"=>"primary_contact_tell",
    "id"=>"primary_contact_tell",
    "value"=>set_value("primary_contact_tell"));
    echo form_input($data);

    ?>
    </td>
    <td width="20">
    <td valign="top">
    <?php

    echo form_label("Address Line 1", "address_line_1");
    $data= array(
    "name"=>"address_line_1",
    "id"=>"address_line_1",
    "value"=>set_value("address_line_1"));
    echo form_input($data);

    echo form_label("Address Line 2", "address_line_2");
    $data= array(
    "name"=>"address_line_2",
    "id"=>"address_line_2",
    "value"=>set_value("address_line_2"));
    echo form_input($data);

    echo form_label("Suburb", "suburb");
    $data= array(
    "name"=>"suburb",
    "id"=>"suburb",
    "value"=>set_value("suburb"));
    echo form_input($data);

    echo form_label("City", "city");
    $data= array(
    "name"=>"city",
    "id"=>"city",
    "value"=>set_value("city"));
    echo form_input($data);

    echo form_label("Postal Code", "postalcode");
    $data= array(
    "name"=>"postalcode",
    "id"=>"postalcode",
    "value"=>set_value("postalcode"));
    echo form_input($data);

    ?>
    </td>
        </tr>
            </table>
<br>
    <p>
        <input type="submit" value="Add New Record">
    </p>
<br>
<?php 
    echo form_close();
?>

任何建议都非常感谢一如既往。如果您知道我可以遵循的完整工作示例或教程,请提供建议。

再次感谢,

4

1 回答 1

2

好的,这会给你提示

     <script type="text/javascript" >
      function registerForm(id)
    {
      $('#user_error').html('');    


  var name        = $.trim($('#name').val());


   if(name=='')
  {
      $('#user_email_error').html('All fields are required!');
      return false;
  }


  $.ajax({
  type  :   'POST',
  data    :   $('#form-register').serialize(),
  url       :   '<?=base_url()?>login/register_user',
  cache : false,    
  success: function(data){
        if(data=='name')
        {
            $('#user_error').html('This name is already in use try new one!');
            return false;   
        }



        else 
        {                   
            document.location.href='<?=base_url()?>login';
        }
      }

      });

  return false;

    }
      </script>

这个在你的控制器中

     function register_user()
{
    $str = '';

    $user_email =   $this->input->post('email');
    $name=  $this->input->post('name');
    $data_a['name']         = $name;

        $result1     =      $this->login_model->check_username($name);

        if($result1->num_rows() > 0 )
        { 
            $str = 'name';   
        }


    else
    { 
        $this->login_model->add_user($data_a);

        $str = 'goo';


     }
     echo($str);


     exit;
}

这是视图

     <div id="user_error" style="color:#FF0000; font-size:13px; font-weight:bold"></div>
    <form method="post" action="" id="form-register" title="Register" onSubmit="return registerForm(this)">


            <input type="text" name="name" id="name" value="" class="input-unstyled" placeholder="Your name" autocomplete="off">



                    <button type="submit" id="send-register">Register</button>
           </form>
于 2013-03-02T11:31:54.750 回答