使用动态创建的选择列表打印相关细节

时间:2013-07-30 10:27:54

标签: codeigniter jquery

我正在编写一个模块,我必须在其中打印该类别。

首先,我已编码过滤您想要搜索的基础(ID / NAME),然后根据用户选择的此信息,动态创建选择列表。

这是问题位:这个动态创建的选择列表,我想要打印 它是表单字段中的相关数据(使用where子句)。我没有得到该领域的回声。其余部分(动态选择列表框)出现;

以下是我的观点:

<div class="navbar">
    <div class="navbar-inner">
        <h2>Show Category</h2>
        <?php
            $dropdown_category= array(  
                'c_id'=>'Category Code',
                'c_name'=>'Category Name');
            echo form_dropdown('clas',$dropdown_category,'','id="clas"');
        ?> 
        <div id="name_sec">
            <!-- PRONT DROP DOWN HERE !-->
        </div>
    </div>
    <br/>
    <div class="navbar-inner">
        <br/> 
        <div id="name_sec2">
            <!-- PRONT form fields  HERE !-->
        </div> 
    </div>
    <div class="navbar"></div>
</div>

我的控制器:

public function category_display()
    {

     //Checking so that people cannot go to the page directly.

    $this->load->helper('form');
    $this->load->database();
       $tabel_for_this="its_category";

    $data_toprint='';
     $id=$this->input->post('send_data_post');



        $query = $this->db->query("SELECT ".$id." FROM ".$tabel_for_this." ");


       foreach ($query->result_array() as $r)
    {


        $dropdown_category2[$r[$id]] = $r[$id];

        } 
            echo form_dropdown('clas2',$dropdown_category2,'','id="clas2"');    
            }

    public function category_display_in_fields()
    {

    $this->load->helper('form');
    $this->load->database();
       $tabel_for_this="its_category";

    $data_toprint='';
     $id=$this->input->post('send_data_post_for_field');

        $sql = "SELECT * FROM ".$tabel_for_this." where '".$id."'= ?";

 $query = $this->db->query($sql,$id);


       foreach ($query->result_array() as $r)
    {
        echo '<input type="text" name="c_dateadded" placeholder="Date" value>

                                 <input type="text" name="'.$r['c_id'].'" placeholder="'.$r['c_name'].'" value>
                                 <select name="c_addedby" placeholder="Added By" value>         
                                 <option>added by</option><option>name1</option><option>name2</option><option>name3</option><option>name4</option>

                                 </select>

                                  <input type="text" name="c_description" placeholder="Description" value> 
                                  <input type="text" name="i_nos." placeholder="Total Items No." value> 
                                  <input type="text" name="c_i_nos_avail" placeholder="Available Stock" value> ';
        } 

    }

这是我的js:

     <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
   <script type="text/javascript">
$(document).ready(function(){


               $("#clas").change(function(){
                     var send_data=$("#clas").val();
                     $.ajax({
                        type:"post",
                        url:"category_display",
                        data:"send_data_post="+send_data,
                        success:function(data){
                              $("#name_sec").html(data);

                        }

                     });
               });
           });
</script>
<!-- CODE TO PRINT DATA IN FIELDS!-->
   <script type="text/javascript">
$(document).ready(function(){


               $("#clas2").change(function(){
                     var send_data2=$("#clas2").val();
                     $.ajax({
                        type:"post",
                        url:"category_display_in_fields",
                        data:"send_data_post_for_field="+send_data2,
                        success:function(data2){
                              $("#name_sec2").html(data2);

                        }

                     });
               });
           });
</script>

只是为了测试目的我在控制器中编写了与数据库相关的内容

1 个答案:

答案 0 :(得分:0)

where子句中的列和变量都是相同的:

$id=$this->input->post('send_data_post_for_field');
$sql = "SELECT * FROM ".$tabel_for_this." where '".$id."'= ?";
$query = $this->db->query($sql,$id);

因此返回的查询将执行以下操作:

SELECT * FROM table WHERE var = var

哪个错了。

它应该是这样的:

$sql = "SELECT * FROM ".$tabel_for_this." where id= ?";