简体   繁体   English

编辑 img 标签以匹配从 SQL 导入的 select 中的值

[英]editing img tag to match value in select which is imported from SQL

I have a program which changes the value of options in a select based on another select.我有一个程序可以根据另一个选择更改一个选择中选项的值。 The values of the select are taken from a database which contains all the information needed.选择的值取自包含所有所需信息的数据库。 I want to change the image tag of my index.php page to match the link to an the selected image (stored in the database).我想更改我的 index.php 页面的图像标签,以将链接与所选图像(存储在数据库中)相匹配。 I have tried and failed to change the img src to match the relevant link.我曾尝试更改 img src 以匹配相关链接,但未能成功。

Does anyone know of any existing questions which may match what I need or even a way to do this?有谁知道任何可能符合我需要的现有问题,甚至是一种方法? I will provide all the code used below.我将提供下面使用的所有代码。


index.php code: index.php 代码:

<div class="">
        <label>Folder:</label>
        <select name="dir" id="dir">
            <option value=''>------- Select --------</option>
            <?php 
            $sql = "select * from `dir`";
            $res = mysqli_query($con, $sql);
            if(mysqli_num_rows($res) > 0) {
                while($row = mysqli_fetch_object($res)) {
                    echo "<option value='".$row->id."'>".$row->directory."</option>";
                }
            }
            ?>
        </select>

        <label>Image Number:</label>
        <select name="images" id="images"><option>------- Select --------</option></select>
    </div>

    <img id="img" src=""/>

js.js code: js.js 代码:

$(document).ready(function() {
    $("#dir").change(function() {
        var image_id = $(this).val();
        if(image_id != "") {
            $.ajax({
                url:"get-folders.php",
                data:{c_id:image_id},
                type:'POST',
                success:function(response) {
                    var resp = $.trim(response);
                    $("#images").html(resp);
                }
            });
        } else {
            $("#images").html("<option value=''>------- Select --------</option>");
        }
    });
});

get-folders.php code: get-folders.php 代码:

<?php include("db.php"); ?>
<?php
if(isset($_POST['c_id'])) {
    $sql = "select * from `images` where `image_id`=".mysqli_real_escape_string($con, $_POST['c_id']);
    $res = mysqli_query($con, $sql);
    if(mysqli_num_rows($res) > 0) {
        echo "<option value=''>------- Select --------</option>";
        while($row = mysqli_fetch_object($res)) {
            echo "<option value='".$row->id."'>".$row->name."</option>";
        }
    }
} else {
    header('location: ./');
}
?>

You need to store the url of the image in the options like this:您需要将图像的 url 存储在如下选项中:

echo "<option value='".$row->id."' data-src='".$row->url."'>".$row->name."</option>";//data-src is custom attribute

Then you can access the data-src attribute with jQuery:s data method(add this to your document.ready function after the dir change callback):然后您可以使用 jQuery:s data 方法访问 data-src 属性(在 dir 更改回调后将其添加到您的 document.ready 函数中):

$("#images").change(function() {
    $('#img').prop('src',$('#images option:selected').data('src'));// add this to top of your change function for the images select box
});

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

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