简体   繁体   English

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

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

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

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: 我的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> 

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

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

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 : 第2步 :

Now you can use your own JS : 现在您可以使用自己的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