【问题标题】:How to display the selected value in a multiple dropdownlist?如何在多个下拉列表中显示选定的值?
【发布时间】:2020-03-06 19:16:07
【问题描述】:

我创建了一个表单来选择要在我的页表中显示的值。下面是我选择多个值并搜索显示的代码,但它没有在多个下拉列表中显示选定的值(类型和日期)。任何人都可以帮我解决这个问题吗?谢谢。

我的前端编码:

       <div class="box inverse">
        <div class="row">
       <div class="col-lg-12">
        <header>
            <h5>Search</h5>
        </header>
        <form id="transaction_search">
            <div class="form-group">
                <div class="col-lg-12">
                    <div class="col-lg-3">
                    </div>
                    <div class="col-lg-12">


                        <div class="form-group">
                             <div class="col-lg-12">
                                 <label for="text1" class="form-group control-label col-lg-2"><?php echo $language['type']; ?>:</label>
                                <div class="col-lg-5">
                                <select id="select_type" class="form-group form-control required"">
                                <option value="transfer" selected><?php echo $language["transfer"]; ?></option>
                                <option value="withdraw"><?php echo $language["withdraw"]; ?></option>
                                <option value="upgrade"><?php echo $language["upgrade"]; ?></option>
                                <option value="register"><?php echo $language["register"]; ?></option>
                                <option value="receive"><?php echo $language["receive"]; ?></option>
                            </select>
                                </div>
                            </div>
                        </div>



               <div class="col-lg-12 form-group">
                            <label for="text1" class="form-group control-label col-lg-2">Date Range:</label>
                            <div class="col-lg-2">
                                <?php echo custom_period_opt(); ?>
                            </div>
                            <label for="text1" class="form-group control-label col-lg-2">Date Created:</label>
                            <div class="col-lg-2">
                                <input type="text" class="form-group form-control datepicker" id="start_date" name="start_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
                            </div>
                            <label for="text1" class="form-group control-label col-lg-2">To</label>
                            <div class="col-lg-2">
                                <input type="text" class="form-group form-control datepicker" id="end_date" name="end_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
                <button id="search" type="button" class="btn btn-sm btn-primary" onclick="search_('transaction_search','transaction_result','transaction_table')">Search</button>   
                <button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>    
            </div>              
            <div class="body" id="transaction_result" style="overflow:auto;">

            </div><!--body-->
        </form>
    </div>
</div>

我的后端编码(这是编码的一部分我尝试选择“撤回”选项来测试输出,但没有在表格中显示任何数据。此编码是要选择“撤回”并选择我选择的内容“日期”):

    <?php
    foreach ($_POST as $key => $value) {
$_POST[$key] = trim(preg_replace('/\s+/', ' ', ($value)));
 }
 $arr_val = $_POST;
 $loc = $arr_val['loc'];
  $action = $arr_val['action'];

 $select_type = $_POST['select_type'];
 unset($arr_val['loc']);
unset($arr_val['action']);
unset($arr_val['select_type']);

$tbl_name = 'withdrawal_record';
if ($action == 'search' && $select_type == 'withdraw' ) {
if ($_POST['select_type'] != '' || $_POST['start_date'] != '' || $_POST['end_date'] != '' ) {
    $sql = 'SELECT * FROM ' . $tbl_name . ' WHERE id is not null';

if($_POST['start_date']!='' && $_POST['end_date']!= '') {
            $sql .=' and a.created between "' . date('Y-m-d', strtotime($_POST['start_date'])) . '" and "' . date('Y-m-d', strtotime($_POST['end_date'])) . '"';
        }

    $result_arr['sql'] = $sql;
    $result_arr = get_data_list($sql);

    $i = 1;
    echo '<table id="dataTable_1" class="dataTable table table-bordered table-condensed table-hover table-striped" style="padding:0px;" border="1">
            <thead>
                <tr>
                     <th>No</th>
                     <th>Date</th>
                     <th>Amount</th>

                </tr>
            </thead>
            <tbody>';

    foreach ($result_arr as $rs_search) {


        echo "<tr>";
        echo "<td>" . $i++ . "</td>";
        echo "<td>" . $rs_search['created'] . "</td>";
        echo "<td>" . $rs_search['withdraw_amount'] . "</td>";
        echo '</td>';
        echo "</tr>";
    }
    echo '</tbody>';
    echo '</table>';
   }
  }


   ?>

下面是jquery函数:

function search_(form_id, div_id, act_file) {
var action = 'search';
var extra = '&action=' + action;
var serialized = $('#' + form_id).serialize();
var form_data = serialized + extra;

$.ajax({
    //dataType: 'json',
    type: 'POST',
    url: '?f=' + act_file,
    data: form_data,
    beforeSend: function() {
        show_overLay();
        $('#' + div_id).html('');
    },
    success: function(data) {
        hide_overLay('');
        if (data) {
            $("#" + div_id).append(data);
            $('.dataTable').dataTable({
                pageLength: 25,
                destroy: true
            });
        } else {
            hide_overLay("Please fill in the field.");
        }
        //console.log(data);
    }
});
}

下面是我的“withdrawal_record”表:

withdrawal_record

以下是我的输出,并没有显示我选择的数据。实际上我想在 04/11/19 和 07/11/19 之间选择日期,选择类型是“Withdraw”:

Output 1

如果成功,将显示如下输出图片:

Output 2

错误输出:

Output 3

【问题讨论】:

  • 你的 JQuery/Javascript 部分在哪里,也请发布。
  • @PrabhjotSinghKainth 已经在我的问题中进行了编辑。谢谢

标签: php sql


【解决方案1】:

在html中添加多个属性来选择:

<select id="select_type" class="form-group form-control required" multiple>

在 JQuery 中进行以下更改:

删除这些:

var action = 'search';
var extra = '&action=' + action;
var serialized = $('#' + form_id).serialize();
var form_data = serialized + extra;

在 Ajax 请求中: 删除这些行:

data: form_data,

添加这些行:

data:{action:"search","loc":$("#select_type").val().toString()},

在 PHP 中删除这些行:

 foreach ($_POST as $key => $value) {
$_POST[$key] = trim(preg_replace('/\s+/', ' ', ($value)));
 }
 $arr_val = $_POST;
 $loc = $arr_val['loc'];
  $action = $arr_val['action'];

这些行改为:

$loc = $_POST['loc'];
$action = $_POST['action'];
$loc =explode(","$loc );
foreach($loc AS $val)
{
  echo $val; // your locations
}

【讨论】:

  • 感谢您的评论。我可以知道 data:{action:"search","loc":$("#select_type").val().toString()},而不是哪个代码?
  • 编码有误。错误输出你可以看到我的问题
猜你喜欢
  • 2021-04-01
  • 2014-06-06
  • 1970-01-01
  • 2015-09-14
  • 2020-07-10
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多