weixin_33701251 2018-02-13 09:42 采纳率: 0%
浏览 15

PHP Ajax mysql选择onload

I use an Ajax call for the select option which loads when I change the select option. I want the first option to load when I load the page without selecting. I'm using the W3schools to achieve this (https://www.w3schools.com/php/php_ajax_database.asp).

My (W3 schools) working code for this:

function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
} 
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
  document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}

  <select id="gang" class="form-control" name="users" onchange="showUser(this.value)" style="height: auto;">
      <?php
        $sql = "SELECT DISTINCT id, datum FROM analyse ORDER BY date DESC";
       $result = mysqli_query($conn, $sql) or die ("Connection failed: " . $conn->connect_error);      
         while ($row = mysqli_fetch_assoc($result)) {                                                                                    
           $id = $row['id']; 
                    ?>
                    <option value="<?= $id; ?>"><?= $id; ?></option>     
                    <?php } ?>                   
  </select>

Fix was to onload on body and add an id to the option field:

 <body onload="showUser(document.getElementById('analyse').value)">

 <option id="analyse" value="<?= $id; ?>"><?= $id; ?></option>    
  • 写回答

1条回答 默认 最新

  • ℡Wang Yan 2018-02-13 10:05
    关注

    in your tag, add below code:

    <body onload="setTimeOut(showUser(document.getElementById('gang').value),1000)">
    

    This will call your function with the first selected value of your select.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?