dragon5006 2014-11-16 13:53
浏览 20
已采纳

HTML表单需要知道DB中有多少条记录

I have a HTML form (PHP), that has one or two checkboxes depending on what a user selects in a dropdown earlier in the form. The problem is:

When a user selects an option from the dropdown, I need to access an SQL DB to find out how many records fit the query and if it exceeds a limit, only allow one checkbox, otherwise 2.

Pseudo:

Select location dropdown (populated by PHP/SQL );
If onchange.location has less than 50 records 
    show/enable 2 type checkboxes
else 
    show/enable one type checkbox

From the research I've done:

Using javascript to access server DB is a no-no,Can't be done on client side using PHP.
  • 写回答

1条回答 默认 最新

  • dougan0529 2014-11-18 15:47
    关注

    When a user selects a user in the dropdown list, a function showUser() is triggered by the onchange event.

    HTML Page:

    <html>
    <head>
    <script>
    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 (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","getuser.php?q="+str,true);
      xmlhttp.send();
    }
    </script>
    </head>
    <body>
    
    <form>
    <select name="users" onchange="showUser(this.value)">
    <option value="">Select a person:</option>
    <option value="1">Peter Griffin</option>
    <option value="2">Lois Griffin</option>
    <option value="3">Joseph Swanson</option>
    <option value="4">Glenn Quagmire</option>
    </select>
    </form>
    <br>
    <div id="txtHint"><b>Person info will be listed here.</b></div>
    
    </body>
    </html>
    

    AJAX request sent to getuser.php, so you can do request to your DB.

    Code of getuser.php:

    <?php
    $q = intval($_GET['q']); // your sent parameter by AJAX
    
    // do your request and process all needed info from DB.
    
    ?php>
    

    You can find all needed info in this tutorial.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100