简体   繁体   English

显示 PHP MySQL 中的选定选项

[英]Display selected option in PHP MySQL

I wants to display the selected program option (selectedprg) and perform on the COUNT of the program (selectedcount) but currently there is no data display after click the button.我想显示选定的程序选项(selectedprg)并执行程序的计数(selectedcount),但目前单击按钮后没有数据显示。 Please assist on this issue.请协助解决这个问题。 Thank you for your time.感谢您的时间。

Dropdown selection下拉选择

<select id="prg" name="prg"  class="demoInputBox" style="padding: 3px; width: 350px;">
    <option value="">Select Programme</option>

        <?php
        //"SELECT * FROM marketing_data WHERE intake_year = '" . $_GET['intake_year'] . "'";
        $sql = "SELECT DISTINCT student_prg FROM marketing_data";
        $do = mysqli_query($conn, $sql);
                          while($row = mysqli_fetch_array($do)){
                              echo '<option value="'.$row['student_matric'].'" data-count="'.$row['count'].'">'.$row['student_prg'].'</option>';
                          }
                      ?>
    </select>

Display data after click button点击按钮后显示数据

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "The next intake for " +selectedprg + " will have " +selectedcount+ " students";
}
</script>

Javascript Javascript

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){

$("#prg").change(function(){
 var selectedprg = $('#prg option:selected').text(); 
var selectedcount = $('#prg option:selected').data('count');

</script>

screencapture截屏

Some things to note:需要注意的一些事项:

  • selectedprg and selectedcount are not globally available, you can't access them in your function. selectedprgselectedcount不是全局可用的,您无法在 function 中访问它们。 Move/declare them outside.将它们移动/声明到外面。
  • Your $(document).ready(function(){ and $("#prg").change( function(){ are missing their closing }) parts.您的$(document).ready(function(){$("#prg").change( function(){缺少它们的结束})部分。

Use your browsers DevTools console to check for errors.使用您的浏览器 DevTools 控制台检查错误。

Try this:尝试这个:

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
  var selectedprg = '';
  var selectedcount = 0;

  function myFunction() {
    document.getElementById("demo").innerHTML = "The next intake for " +selectedprg + " will have " +selectedcount+ " students";
  }

  $(document).ready(function(){
    $("#prg").change( function(){
      selectedprg = $('#prg option:selected').text(); 
      selectedcount = $('#prg option:selected').data('count');
    });
  });
</script>

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

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