如何基于下拉选择通过单选按钮显示数据?

库珀

我正在尝试动态生成单选按钮,并在其前面添加数据。要显示在单选按钮前面的数据是基于下拉选择的,该下拉选择还会使用javascript在文本框中显示一些数据。

我尝试将选定的选项放在字符串中,并在下一个查询中使用它,但是我知道我做错了。

数据库连接

$db = pg_connect("");
$query = "select account_name,account_code,address1,address2,address3 FROM 
customers";
$result = pg_query($db,$query);

//新查询

 $sql1= "select name from conferences";
      $result1= pg_query($db, $sql1);

//结束

//新代码

 <select class="form-control" id="conference"  name="conference">
 <option value="">Select Conference...</option>
 <?php while($rows1 = pg_fetch_assoc($result1)) { ?>
    <option value="<?= $rows1['code']; ?>"><?= $rows1['name']; ?></option>
 <?php } ?>
 </select>
                <br>

//新代码的结尾

下拉菜单选择数据。

<select onchange="ChooseContact(this)" class="form-control" 
id="account_name"  name="account_name" >

<?php
while($rows= pg_fetch_assoc($result)){ 
echo  '<option value=" '.$rows['address1'].'  '.$rows['address2'].' 
'.$rows['address3'].''.$rows['account_code'].'">'.$rows['account_name'].' 
'.$_POST[$rows['account_code']].' 
</option>';
}?>
</select> 

使用javascript根据选择的值在文本区域中显示数据。(代码可以正常工作到这里)

<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;"value=""placeholder="Address..."> 
</textarea>
                <script>
                function ChooseContact(data) {
document.getElementById ("comment").value = data.value;
            }
                </script> 

根据所选选项在单选按钮前面显示数据(如果我在查询中使用一些随机值,则此代码有效,但如果我使用上一个查询中的所选值“ account_code”,则此代码无效。我使用POST GET方法携带所选值)

 <?php

//新代码

 $sql = "select  order_number, order_date from orders where 
 customer_account_code =  '3000614' and conference_code='DS19-'"; <-Data 
 gets displayed when put random value like this.

 $code = $_GET[$rows['account_code']];
 $conf = $_GET[$rows1['conference_code']];
 $sql = "select  order_number, order_date from orders where 
customer_account_code = '$code' and conference_code= '$conf']"; <- But I 
want to display the data against the selected value, i.e, the 'account_code' 
in the variable $code from the dropdown select 

//结束

$res = pg_query($db,$sql);

while($value = pg_fetch_assoc($res) ){
echo "<input type='radio' name='answer' 
value='".$value['order_number']." ".$value['order_date']."'>" 
.$value['order_number'].$value['order_date']." </input><br />";
                    }
                    ?>

我需要帮助找到一种方法,将选定的“ account_code”放入变量中,并在$ sql查询中使用它。

赛义夫·曼威尔

请尝试使用以下代码:(对我来说有用)

1-将此行添加到您的HTML中<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>

2-将您的代码编辑为此:

下拉菜单选择数据:

<select class="form-control" id="account_name"  name="account_name">
    <option value=""></option>
    <?php while($rows = pg_fetch_assoc($result)) { ?>
        <option value="<?= $rows['address1'].' '.$rows['address2'].' '.$rows['address3'].'-'.$rows['account_code']; ?>"><?= $rows['account_name']; ?></option>
    <? } ?>
</select>

使用jQuery根据所选值在文本区域中显示数据:

<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;" value="" placeholder="Address..."></textarea>

jQuery代码:

<script type="text/javascript">
    $('#comment').val($('#account_name').val()); // MAKE A DEFAULT VALUE
(function($) {
    $('#account_name').change(function() {
        $('#results').html(''); // REMOVE THE OLD RESULTS 
        var option = $(this).val();
        $('#comment').val(option);
        // EDIT RADIO WITH AJAX 
        $.ajax({
            type: "POST",
            url: "path/test.php",
            dataType:'JSON',
            data: $('#account_name').serialize()
        }).done(function(data) {
            for (var i = 0; i < data.length; i++) {
                // ADD RADIO TO DIV RESULTS
                $('#results').append('<input type="radio" name="answer" value="'+data[i].order_number+'">'+data[i].order_date+'</input><br>');
            }
        });
    });
})(jQuery);
</script>

之后,将此HTML添加到您的页面,以显示来自AJAX数据的结果

<!-- RADIOs -->
<div id="results"></div>

3-创建一个新文件,如path / test.php

在此文件中,使用此CODE返回带有JSON的:)

<?php
header('Content-type: application/json');

// CONNECT (JUST USE YOUR CUSTOM CONNECTION METHOD & REQUIRE CONFIG FILE IF YOU WANT)
$db = pg_connect("");

$value = explode('-', $_POST['account_name']);

// EXPLODE AND GET LAST NUMBER AFTER < - >
$code = (int) end($value);

$sql = "select order_number, order_date from orders where customer_account_code =  '$code'";

$res = pg_query($db, $sql);

// CREATE JSON RESULTS
$is = '';
while($data = pg_fetch_assoc($res)) {
    $is .= json_encode($data).', ';
}

// AND GET ALL 
echo '['.substr($is, 0, -2).']';
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

下拉列表基于单选按钮显示

如何基于单选按钮选择显示和隐藏输入字段

使用jQuery基于下拉选择选择单选按钮

如何基于从Java中的下拉列表或单选按钮的选择添加textArea?

如何基于下拉菜单选择自动使用数据库中的数据填充表单

显示基于下拉菜单选择的模式弹出窗口

选择下拉列表并单击单选按钮时显示div

显示基于单选按钮选择的一些控件

Rails 4形式:基于单选按钮选择的条件字段显示

基于 jQuery 中的单选按钮选择显示和隐藏内容

基于使用Python Tkinter的多个单选按钮选择的显示列表

如何通过 Flask 中的下拉菜单选择数据帧索引?

基于单选按钮选择的计算

如何基于单选按钮选择创建动态导航

如何加载基于单选按钮选择的 Angular 组件

基于单选按钮选择如何更改 powerapps 中的放置选项

如何基于选定的单选按钮更新选择选项?

如何使用Jquery基于单选按钮显示<div>内容

如何基于角度6中的单选按钮显示/隐藏div?

如何基于单选按钮类显示/隐藏div?

显示从动态创建的单选按钮模板中选择的数据

根据单选按钮的选择在HTML表或DataTable中显示数据

基于单选按钮选择的隐藏div具有下拉菜单

如何基于其他单选按钮组值显示并向单选按钮组添加验证

如何显示基于上一个单选按钮的单选按钮集

如何通过单击其 div 来选择单选按钮?

选择单选按钮后的下拉列表

如何根据选择的单选按钮交换显示的图像?

用户选择单选按钮时如何显示不同的内容?