繁体   English   中英

如何在Codeigniter中将jQuery值传递给php模型函数

[英]How to pass jquery value to php model function in codeigniter

我需要将jquery ID值传递到codeigniter中的est_model-> get_img()函数中,并在同一模式框中显示所有内容,我尝试了自己的逻辑,但它不起作用,请帮助大家,提前谢谢

这是我的代码如下:模态标签:

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

我的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);
    }});

});

模式框:

 <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> 

除非模式框实际上是admin/est_view.php否则您的PHP文件无法从GET请求中读取ID,但是我想我知道您要尝试做的事情,

第1步 :

创建一个名为admin/est_view.php的文件,该文件仅包含php脚本,该脚本将从数据库和foreach clause提取数据以形成html响应,并且不要忘记它需要连接到数据库。

这是您可以使用或开发的示例:

<?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);
?>

第2步 :

现在您可以使用自己的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);
    }});

});

暂无
暂无

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

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