【问题标题】:How to pass jquery value to php model function in codeigniter如何将jquery值传递给codeigniter中的php模型函数
【发布时间】:2019-03-11 14:48:41
【问题描述】:

我需要将 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>

【问题讨论】:

    标签: javascript php jquery html codeigniter


    【解决方案1】:

    除非模态框实际上是 admin/est_view.php,否则您的 PHP 文件无法从 GET 请求中读取 ID 但是我想我明白您要做什么,

    第 1 步:

    创建一个名为 admin/est_view.php 的文件,其中仅包含将从 DB 中提取数据的 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);
        }});
    
    });
    

    【讨论】:

    • @Kynjai 我希望你能接受我的回答,如果你觉得它有帮助;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2017-10-19
    • 2013-05-02
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多