dplht39359
2018-10-06 07:28
浏览 81

如何在codeigniter中将jquery值传递给php模型函数

i need to pass jquery ID value in to my est_model->get_img() function in codeigniter and display all the content in the same modal box, i tried my own logic it doesn't work, please help guys, thank you in advance

here is my code below: modal a tag:

<a href="#view_all_img_modal" data-toggle="modal" data-est-id="<?php echo $row->est_id;?>">View all</a>

my jquery:

$('#view_all_images_modal').on('show.bs.modal', function(e) {
    var est_id = $(e.relatedTarget).data('est-id');
    $.ajax({url:"admin/est_view.php?ID="+est_id,cache:false,success:function(result){
        $("#view_all_img_modal").html(result);
    }});

});

modal box:

<div class="modal fade" id="view_all_img_modal" tabindex="-1" role="dialog">
<div class="row">
    <?php
        $id=$_GET['ID'];
            $est_img=$this->est_model->get_img($id);
            if($est_img):
                foreach ($est_img as $img):
                $ser_img=($img->img_name==NULL)?'no-image.png':$img->img_name;?>
                    <img src="<?php echo base_url('../public/images/rel_img/'.$ser_img);?>">
                <?php endforeach; 
            endif;?>
</div>
</div>

</div>

图片转代码服务由CSDN问答提供 功能建议

我需要将jquery ID值传递给codeigniter中的est_model-&gt; get_img()函数并显示所有 在同一模态框中的内容,我尝试了自己的逻辑它不起作用,请帮助伙伴,提前谢谢

这里是我的代码: \标记:

 &lt; a href =“#view_all_img_modal”data-toggle =“modal”data-est-id =“&lt;?php echo $ row-&gt; est_id;?&gt;  “&gt;查看全部&lt; / a&gt; 
   
 
 

我的jquery:

  $('#view_all_images_modal')  .on('show.bs.modal',function(e){
 var est_id = $(e.relatedTarget).data('est-id'); 
 $ .ajax({url:“admin / est_view  .php?ID =“+ est_id,cache:false,success:function(result){
 $(”#view_all_img_modal“)。html(result); 
}}); 
 
}); 
    
 
 

模态框:

 &lt; div class =”modal fade“id =”view_all_img_modal“tabindex =” -  1“role =”dialog“&gt; 
 
&lt; div class =”row“&gt; 
 
  &lt;?php 
 
 $ id = $ _ GET ['ID']; 
 
 $ \ test_img = $ this-&gt; \ test_model-&gt; get_img($ id); 
 
 if($ est_img)  :
 
 foreach($ est_img as $ img):
 
 $ ser_img =($ img-&gt; img_name == NULL)?'no-image.png':$ img-&gt; img_name;?&gt  ; 
 
&lt; img src =“&lt;?php echo base_url('../ public / images / rel_img /'.$ ser_img);?&gt;”&gt; 
 
&lt;?php endforeach;  
 
 ENDIF;?&GT; 
 
&LT; / DIV&GT; 
 
&LT; / DIV&GT;   
 
  
 
  \  r 
 
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpwdldgn43486 2018-10-06 07:39
    已采纳

    Your PHP file can't read the ID from GET request unless the modal box is in fact admin/est_view.php However I think I understand what u'r trying to do,

    STEP 1 :

    create a file called admin/est_view.php that contain just the php script that will extract the data from DB and the foreach clause to form html response, and don't forget it needs to connect to the database.

    Here's an example u can use or develop :

    <?php
        try{
            $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "passwd");
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $e){
            die("ERROR: Could not connect. " . $e->getMessage());
        }
        try{
            if(isset($_REQUEST["ID"])){
                $sql = "SELECT * FROM images_table WHERE :ID";
                $stmt = $pdo->prepare($sql);
                $term = $_REQUEST["ID"] . '%';
                $stmt->bindParam(":ID", $term);
                $stmt->execute();
                if($stmt->rowCount() > 0){
                    while($row = $stmt->fetch()){
                        echo "<img src=\"" . $row["url"] . "\">";
                    }
                }
            }  
        } catch(PDOException $e){
            die("ERROR: Could not able to execute $sql. " . $e->getMessage());
        }
        unset($stmt);
        unset($pdo);
    ?>
    

    STEP 2 :

    Now you can use your own JS :

    $('#view_all_images_modal').on('show.bs.modal', function(e) {
        var est_id = $(e.relatedTarget).data('est-id');
        $.ajax({url:"admin/est_view.php?ID="+est_id,cache:false,success:function(result){
            $("#view_all_img_modal").html(result);
        }});
    
    });
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题