【问题标题】:how to display data in input tag after selecting in select tag value?选择标签值后如何在输入标签中显示数据?
【发布时间】:2021-05-09 16:01:56
【问题描述】:

我想在我的选择标签中选择后实现,它会在我的模态中显示一个输入标签,并且已经在数据库的其他表中获取。

totalfees.php

if(isset($_POST["year"]))  
{  
    $output = '';  
    $query = "SELECT total_fees FROM manage_fees WHERE year_lvl = '".$_POST["year"]."'";  
    $result = mysqli_query($connect, $query);    
    while($row = mysqli_fetch_array($result))  
    {  
        $output .= '  
                <input type="text" class="form-control" id="tf" name="tf" readonly="readonly" value="'.$row["total_fees"].'">
                ';  
    }  
    echo $output;  
} 

gettotal.js

$(document).ready(function(){  
$('#year').click(function(){  
     var year = $(this).attr("year");  
     $.ajax({  
          url:"../include/assessment.php",  
          method:"post",  
          data:{year:year},  
          success:function(data){  
               $('#total_fees').html(data); 
          }  
     });  
});  

assessment.php

<div class="modal fade" id="fee_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Assess Student</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form method="POST" action="officer_studentAssessment.php" id="reg">
                    <div class="form-group">
                        <label for="year">Year</label>
                        <select class="form-control" id="year" name="year" required>
                            <?php
                                $result = $connect->query("SELECT * FROM year_lvl") or die($connect->error());
                                while($row = $result->fetch_assoc()):
                            ?>
                                <option value="<?php echo $row['year']; ?>"><?php echo $row["year"]; ?></option>
                            <?php endwhile; ?>
                        </select>
                    </div>
                    <div class="form-group" id="total_fees">
                        <label for="tf">Total fees</label>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-success" name="create" id="create" type="submit">Create</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>    

totalfees.php中,输出应该显示在id = total_fees的DIV标签上,问题是我点击选择标签后DIV消失了

<div class="form-group" id="total_fees">
    <label for="tf">Total fees</label>
</div>

【问题讨论】:

  • 警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!
  • 嗨,$(this).attr("year") 应该是 $(this).val()
  • @Swati 在选择标签中选择后仍然没有显示新的 div 标签
  • 你试过下面的答案了吗?
  • 嘿@JanbresGagaracruz - 跟进。你的问题得到了满意的回答吗?如果我们可以提供更多帮助,请在任何答案下方添加评论,或编辑您的问题以澄清您还想知道什么。 否则,请选择“最佳答案”(通过单击答案旁边的复选标记)来结束问题。如果没有答案提供有用的信息,请添加您自己的答案并选择它作为最佳答案(关闭问题)。也请记住:您可以投票任何您认为有帮助的答案(如果需要,您也可以投票并勾选相同的答案。)*谢谢!

标签: php mysql ajax


【解决方案1】:

添加

<scritp>
let selectval = document.getElementById('selectId');
let val =document.getElementById('yourId');
let val.value = selectval
</script>

在 slectId 中添加您选择的输入的 id,并在 yourId 中添加您想要添加数据的输入字段的 id

【讨论】:

    【解决方案2】:

    看起来像是错字。

    在 gettotal.js 中,在您的 ajax 代码块中,您调用了错误的 url。

    代替:

    url:"../include/assessment.php", 
    

    尝试:

    url:"../include/totalfees.php", 
    

    另外,您可能想要更改:

    $('#year').click(function(){
    

    $('#year').change(function(){
    

    【讨论】:

      猜你喜欢
      • 2017-08-21
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2019-05-28
      • 2021-10-30
      • 1970-01-01
      相关资源
      最近更新 更多