选择表单后从数据库中获取隐藏的输入值 [英] Get hidden input value from database after select form

查看:63
本文介绍了选择表单后从数据库中获取隐藏的输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择输入,从数据库retailer_id获取选项,我需要它,当管理员在此选择输入上选择零售商时,我将使用retailer_slug新的,然后,当管理员创建或更新,将数据库中的retailer_id和零售商插件隐藏在输入上。我的代码是:

i have a select input that get options from database retailer_id, what i need it, when the admin select a retailer on this select input, i'll have a new one with the retailer_slug, and then, when the admin create or update, will sen to database the retailer_id and the retailer_slug on hidden input. my code is:

已编辑

        <select class="textbox2" id="retailer_id" name="retailer_id">
<option value="">--- Please select store ---</option>
<?php
    $sql_retailers = smart_mysql_query("SELECT * FROM cashbackengine_retailers WHERE status='active' ORDER BY title ASC");
    while ($row_retailers = mysql_fetch_array($sql_retailers))
    {
        if ($retailer_id == $row_retailers['retailer_id']) $selected = " selected=\"selected\""; else $selected = "";
        echo "<option data-slug=\"".$row_retailers['slug_title']."\" value=\"".$row_retailers['retailer_id']."\"".$selected.">".$row_retailers['title']."</option>";
    }
?>
</select>

<input type="text" name="slug" id="slug" value=""/>
         <script type="text/javascript">
        $(document).ready(function() {
        $('#retailer_id').on('change', function() {
var $selected = $('#retailer_id option:selected');
$('input[name=slug]').val($selected.data('slug'));
});
});
        </script>


推荐答案

如果我理解正确,这是你可以做的一件事执行:

If I understand correctly, this is one thing you could do:

PHP

<select class="textbox2" id="retailer_id" name="retailer_id">
    <option value="">--- Please select store ---</option>
    <?php
        $sql_retailers = smart_mysql_query("SELECT * FROM cashbackengine_retailers WHERE status='active' ORDER BY title ASC");
        while ($row_retailers = mysql_fetch_array($sql_retailers))
        {
            if ($retailer_id == $row_retailers['retailer_id']) $selected = " selected=\"selected\""; else $selected = "";
            echo "<option data-slug=\"".$row_retailers['retail_slug']."\" value=\"".$row_retailers['retailer_id']."\"".$selected.">".$row_retailers['title']."</option>";
        }
    ?>
</select>
<input type="hidden" name="retailer_slug"/>

jQuery

$('#retailer_id').on('change', function() {
    var $selected = $('#retailer_id option:selected');
    $('input[name=retailer_slug]').val($selected.data('slug'));
});

所以基本上,这里的想法是保存 retailer_slug 每个<选项> 上的数据 - 属性值。然后,当更改所选选项时, retailer_slug 值将复制到隐藏输入。

So basically, the idea here is that you save the retailer_slug value as a data- attribute on each <option>. Then when the selected option is changed, the retailer_slug value is copied over to the hidden input.

这篇关于选择表单后从数据库中获取隐藏的输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆