2013-02-27 39 views
0

我是codeigniter嘗試一些簡單頁面的新手。我已成功連接到數據庫以進行插入和更新。但現在我想填充數據庫中的字段進行更新。也就是說,當用戶從下拉列表中選擇名稱時,相應的值應出現在其他字段中,以便用戶可以輕鬆更新。在codeigniter中動態加載表格

我已經在這方面做了沒有ajax或jQuery。它爲我工作,但有一些警告消息 - 使用未定義的常量。

我是由mvc給。幫我清楚這一點。

控制器:

public function update() 
{ 
$this->load->helper('form'); 
$this->load->library('form_validation'); 
$data['title'] = 'Update Type'; 
$data['product_type'] = $this->editType_model->get_product_type(); 
    $data['row'] = $this->editType_model->get_row(); 

$this->form_validation->set_rules('productype', 'productype', 'required'); 

if ($this->form_validation->run() === FALSE) 
{ 
    $this->load->view('templates/header', $data); 
    $this->load->view('cm/update', $data); 
    $this->load->view('templates/footer'); 

} 
else 
{ 
     $data['title']= 'Updation'; 
     $data['message']= 'Updation succeeded'; 
    $this->editType_model->update_news(); 
    $this->load->view('cm/success', $data); 
} 
} 

型號:

public function get_product_type() { 
    $data = array(); 
      $this->db->order_by("product_type_name", "desc"); 
    $query = $this->db->get('product_type'); 
    if ($query->num_rows() > 0) { 
     foreach ($query->result_array() as $row){ 
       $data[] = $row; 
      } 
    } 
    $query->free_result(); 
    return $data; 
} 
public function get_row() { 
    $data = array(); 
       $id= $_POST[product_type_id]; 
    $query = $this->db->get_where('product_type', array('product_type_id' => $id)); 
      if ($query->num_rows() > 0) { 
     foreach ($query->result_array() as $row){ 
       $data[] = $row; 
      } 
    } 
    $query->free_result(); 
      return $data; 

} 
public function update_news() 
{ 
$this->load->helper('date'); 
$Product = $this->input->post('product_type_id'); 
$data = array(
    'product_type_name'=>($_POST['productype']), 
    'rank'=>($_POST['rank'])   
      ); 
$this->db->where('product_type_id',$Product); 
return $this->db->update('product_type', $data); 
} 

查看:

<?php $productType = 0; 
if(isset($_POST['product_type_id'])){ 
$productType = $_POST['product_type_id']; 
}?> 
<div id="Content"><div> 
<form action="" method="post" name="f1"> 
<table ><tr><td>Select Product Type Name:</td> 
    <td><Select id="product_type_id" name="product_type_id" onChange="document.f1.submit()"> 
      <option value="0">--SELECT</option> 
      <?php if (count($product_type)) { 
           foreach ($product_type as $list) { ?> 
      <option value="<?php echo $list['product_type_id']; ?>" 
      <?php if($productType==$list['product_type_id']) echo  "Selected" ?> > 
      <?php echo $list['product_type_name']; ?></option> 
      <?php }} ?></Select></td></tr></table></form> 
<?php if($productType){ 
      if (count($row)) { 
     foreach ($row as $faProductType) { ?> 
    <div > <form action="" method="post" class="form-Fields" > 
    <input type="hidden" name="product_type_id" id="product_type_id" value="<?php echo  $faProductType['product_type_id']; ?>" > 
    <table border="0" cellpadding="8" cellspacing="0" > 
    <tr><td>Product Type <font color="red">*</font></td><td style="width: 10px;"> :   </td> 
    <td><input name="productype" type="text" value="<?php echo  $faProductType['product_type_name']; ?>" style=" width:300px;" /></td> 
      <td><span class="err" id="productTypeDesc1Err"></span></td></tr> 
     <tr><td >Rank</td><td style="width:10px;"> : </td> 
      <td ><input type="text" name="rank" id="rank" value="<?php echo  $faProductType['rank']; ?>" size="39" />&nbsp;&nbsp;<span class="err"  id="productTypeNameErr"></span></td> 
      <td ></td></tr><tr><td></td><Td colspan="2" align="center"> 
      <input type="submit" value="Update" class="buttonimg"  name='cm/editType/update' style="width:100px" 
      onClick="return validEditProductType();"> 
      </Td><td></td></tr></table></form></div></div></div> 
<?php }}} ?> 

回答

0

您可以使用Ajax用於此目的
您的選擇框的onchange填充表單一個div 。

控制器方法

function getUserdata(){ 
    $where['username'] = $this->input->post('username'); 
    $this->load->model('users_model'); 
    $data['user'] = $this->users_model->getUser($where); 
    $this->load->view('userform',$data); 
} 

你的模型方法

function getUser($where){ 
    return $this->db->where($where)->get('usertable')->row() 
} 

阿賈克斯

$(function(){ 
    $('#selecttion').change(function(){ 
     var username = $(this).val(); 
     $.ajax({ 
      url : '<?php 'echo url to controller method'?>', 
      type : 'POST' , 
      data : 'username='+username, 
      success : function(data){ 
       $('#dividtofill').html(data); 
      } 
     }); 

    }); 
}) 

編輯:

調用Ajax是容易
爲此包括在您的標題jQuery庫。
這裏是你的下拉

注意:把上面的Ajax代碼在你的頭一節中的腳本

<select name="user" id="selecttion"> 
    <option value="John">John</option> 
    <option value="Sam">Sam</option> 
    <option value="David">David</option> 
</select> 

現在,當這個下拉值更改上面定義的AJAX功能將被調用。

+0

我可以理解這背後的邏輯。但是你能讓我知道我在我看來如何稱這個ajax。 – Ram 2013-03-01 04:52:52

+0

@Ram查看編輯 – 2013-03-01 06:17:47

+0

我試過沒有ajax ..但有一些警告..檢查我的編輯.. – Ram 2013-03-05 07:27:57

0

我終於清除了錯誤。我通過一個簡單的isset循環完成了這個任務。

現在我的模型中的get_row()看起來像這樣。

public function get_row() { 
      $data = array(); 
$id= 0; 
if(isset($_POST['product_type_id'])){ 
$id = $_POST['product_type_id']; 
} 
$query = $this->db->get_where('product_type', array('product_type_id' => $id)); 
if ($query->num_rows() > 0) { 
foreach ($query->result_array() as $row){ 
$data[] = $row; 
} 
} 
    $query->free_result(); 
    return $data; 
}