我们可以在javascript中嵌入form_dropdown函数吗?

时间:2015-09-19 05:54:54

标签: javascript php jquery mysql codeigniter

您好我正在使用codeigniter。我需要在javascript中嵌入form_dropdown。我有一个带有按钮添加行的表。 当我单击添加时,我复制上面的同一行。在我的视图页面中,我有一个javascript来添加动态表格行。 现在我无法下载它。我有一个控制器文件,我传递的数据应该显示为下拉框。 我的控制器功能:

class Admin_billing extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->load->model('billing_model');

        if(!$this->session->userdata('is_logged_in')){
            redirect('admin/login');
        }
    }

    public function ticket($id)
    {
    $this->load->library('session');
            $this->session->unset_userdata('client_detail');
        $id=$this->uri->segment(4);

        $data['id']=$this->uri->segment(4);

        $data['employee']=$this->billing_model->emp_name();

        $data['main_content'] = 'admin/billing/ticket_page';
        $this->load->view('includes/template', $data);  
     }
}

我的模特功能:

<?php
class billing_model extends CI_Model {
    public function __construct()
    {
        $this->load->database();

    }
public function emp_name()
    {

        $this->db->select('employee.first_name');
    $this->db->from('employee');
    $this->db->group_by('employee.id');
    $query = $this->db->get();

        return $query->result_array(); 
    }


}
?>  

这里我有一个emp_name函数来从我的数据库中获取员工姓名。 我的观点页面:

<script>
function displayResult() {
    var row = document.getElementById("test").insertRow(-1);
    row.innerHTML = '<td>form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"')</td><td><input type="text" name="start_time[]" value="" style="width:35px;"/></td><td><input type="text" name="pid[]" style="width:35px;"/></td><td><input type="text" name="description[]" class="description" value="" style="width:145px;"/></td><td><input type="text" class="type" value="" style="width:45px;"/></td><td><input type="text" class="qty_prch" value="" style="width:45px;"/></td><td><input type="text" class="qty_used" value="" style="width:45px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td>';
}

</script>
<?php

      $attributes = array('class' => 'form-horizontal', 'id' => '');
      $options_employee = array('' => "Select");
      foreach ($employee as $row)
      {
        $options_employee[$row['name']] = $row['name'];
      }

      ?>
<table id="test">
            <thead>
            <tr>
            <td style="width:80px;">Employee</td>
            <td style="width:35px;">Start time</td>
            <td style="width:35px;">Id</td>
            <td style="width:145px;">Description</td>
            <td style="width:45px;">Type></td>
            <td style="width:45px;">qty prch></td>
            <td style="width:45px;">qty used</td>
            <td style="width:70px;">Price</td>
            <td style="width:70px;">discount></td>
            <td style="width:70px;">Tax></td>
            <td style="width:70px;">Total</td>
            </tr>
            </thead>
            <tbody>
            <tr>
            <td><?php echo form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"');?></td>
            <td><input type="text" name="start_time[]" value="" style="width:35px;"/></td>
            <td><input type="text" name="pid[]"  value="" style="width:35px;"/></td>
            <td><input type="text" name="description[]" class="description" value="" style="width:145px;"/></td>
            <td><input type="text" name="type[]" class="type" style="width:45px;"/></td>
            <td><input type="text" name="qty_prch[]" class="qty_prch" style="width:45px;"/></td>
            <td><input type="text" name="qty_used[]" class="qty_used" style="width:45px;"/></td>
            <td><input type="text" name="price[]" class="price" style="width:70px;"/></td>
            <td><input type="text" name="discount[]" class="discount" style="width:70px;"/></td>
            <td><input type="text" name="tax[]" class="tax" style="width:70px;"/></td>
            <td><input type="text" name="total[]" class="total" style="width:70px;"/></td>

            </tr>
            </tbody>

            </table>

            <div id="add_row">
        <button onClick="displayResult()" class="add_r"></button>
                </div>

这是我在上表中的视图页面我的第一个字段是下拉php代码。这适用于我的表中的静态行。当我使用javascript添加新行时,我无法获得下拉列表。如何处理此问题?有人可以帮我编码吗?

1 个答案:

答案 0 :(得分:0)

首先,PHP是服务器端语言。如果你只在javascript中追加html你的行没有执行php。

你需要使用ajax。使用jQuery解决方案:

  1. 在服务器端(php),您需要准备url(函数)以提供新的行剪切数据。
  2. on onon onClick事件触发jQuery ajax http://api.jquery.com/jquery.ajax。 Url param指向准备好的网址。
  3. 完成ajax请求后,将响应数据设置为表格的新行。
  4. $.ajax({
        url: "DOMAIN/newRow",
      })
      .done(function(data) {
          $('table#test tbody').append(data);
        }
      });

    PHP(在您的管理员账单控制器中)

    我不熟悉cakePHP,这么简单。你需要写函数来返回生成的行html。

    public function newRow()
    {
         $options_employee = 'prepare this variable here';
         $dropdown = form_dropdown('employee', $options_employee, set_value('employee[]'), 'class="span2"');
    
         // Send data to browser (return is only for demonstration)
         return '<tr><td>' . $dropdown . '</td><td><input type="text" name="start_time[]" value="" style="width:35px;"/></td><td><input type="text" name="pid[]" style="width:35px;"/></td><td><input type="text" name="description[]" class="description" value="" style="width:145px;"/></td><td><input type="text" class="type" value="" style="width:45px;"/></td><td><input type="text" class="qty_prch" value="" style="width:45px;"/></td><td><input type="text" class="qty_used" value="" style="width:45px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td><td><input type="text" value="" style="width:70px;"/></td></tr>';
    }
    
相关问题