简体   繁体   English

从控制器发送数据以查看Codeigniter

[英]sending data from controller to view codeigniter

I have controller that loads a customer details view which includes the name of the customer, email, fax,phoneNo. 我有一个控制器,可加载客户详细信息视图,其中包括客户名称,电子邮件,传真,电话号码。 ...etc i want the user of the system when types in the form of the customer name to check if this customer exists in the database and instantly to show his contact details in the forms of email, fax , ...etc is this doable without having to load the view once again ? ... etc我希望系统的用户在输入客户名称的形式时检查此客户是否存在于数据库中,并立即以电子邮件,传真等形式显示其联系方式,... etc是是否可以不必再次加载视图?

1.You can do this by having a array of customers when you loading the view, when user types the name you can check this value with array you have. 1.您可以通过在加载视图时拥有一组客户来完成此操作,当用户键入名称时,可以使用您拥有的数组来检查此值。 It is to be noted you have to make js array of the php array you get from the controller. 要注意的是,您必须使从控制器获取的php数组的js数组。

2.Use ajax. 2.使用ajax Simple and dynamic. 简单而动态。

Hope this will help you. 希望这会帮助你。

Controller: [I am writing only the functions.] 管制员:[我只写功能。]

public function customer_details() {
       $data['base_url'] = base_url();
       $data['somedata'] = 'somevalue';
       $this->load->view('customerDetailsView',$data);
}
public function ajax_fetchDetails() {
       $customerName = $this->input->post('cname');
       $this->load->model('customerDetails');
       $cdetails = $this->customerDetails->fetchdetails($customerName);
       if(!empty($cdetails)) {
          echo json_encode($cdetails);
       }
       else echo '0'; //Handle it in view.
}


Model: [Write your own Query] 型号:[编写您自己的查询]

 Select * from `customerdetails` where `name` like $customerName limit = 1; 


View: customerDetailsView [I am writing only what should appear in the tag] 查看:customerDetailsView [我只写标记中应该出现的内容]

<!--For the following script to work you'll have to use jquery library.-->
  <script type="text/javascript">
    $("#fetchbtn").onclick( function(event) {
        var name = jQuery.trim($("#cname").val()); //Fetch the customer name enter in the input field.
        if(name != '') // Check for empty condition
        {
            $.ajax({
            type: "POST", url:"<?php echo $base_url.'<CONTROLLER NAME>/ajax_fetchDetails'; ?>",
            data:{cname:name},
                success: function(result) {
                    if(result != '0') {
                        var customerDetails = JSON && JSON.parse(result) || $.parseJSON(result); // Parse the JSON encoded customer details into string.
                        var email = customerDetails.email, fax = customerDetails.fax; //... So on
                        $("#email").val(email); $("#fax").val(fax); //... So on
                    }
                }
            });
        }
    });
  </script>

... So on Untill all the details are covered. ...因此,直到所有内容都包括在内。

 <!--For the following script to work you'll have to use jquery library.--> <script type="text/javascript"> $("#fetchbtn").onclick( function(event) { var name = jQuery.trim($("#cname").val()); //Fetch the customer name enter in the input field. if(name != '') // Check for empty condition { $.ajax({ type: "POST", url:"<?php echo $base_url.'<CONTROLLER NAME>/ajax_fetchDetails'; ?>", data:{cname:name}, success: function(result) { if(result != '0') { var customerDetails = JSON && JSON.parse(result) || $.parseJSON(result); // Parse the JSON encoded customer details into string. var email = customerDetails.email, fax = customerDetails.fax; //... So on $("#email").val(email); $("#fax").val(fax); //... So on } } }); } }); </script> 

The only disadvantage here is, if two customers have same name only the 1st customer's details will be fetched. 唯一的缺点是,如果两个客户具有相同的名称,则只会提取第一个客户的详细信息。 To avoid this, you'll have to maintain unique username for each customer. 为了避免这种情况,您必须为每个客户维护唯一的用户名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM