doushengyou2617 2012-07-19 03:13
浏览 111

使用一个<form>的选择进入另一个<form>?

From within php, I have a large html <form> filled out with lots rows of patient info from a postgres database. When a doctor double-clicks on a row, it sets a var in $_POST and invokes another php script to read up and display specific info about that row from the database. This all works.

But there are now so many rows of patient data that the doctors don't want to scroll and scroll to find the patient rows they're looking for, they want a patient prefilter <form> so that a click on an element in it will result in the large display filtered to just that patient's rows.

What's a basic approach to doing this? I'm a newb; I'm currently using html, php, and some javascript.

  • 写回答

1条回答 默认 最新

  • dqp21271 2012-07-19 03:46
    关注

    Make a second form with whatever options you'd like to filter on, this part will be specific to your data but you want something like

    <form id="search-form">
      <label>Name:</label><input type="text" name="patient-name"></input>
    </form>
    

    You'll need to build a query string (and make sure you use GET, because that will make things easier for you). This will require tweaking if you want to use radio buttons, or something similar, but here's the general idea:

    function getSearchParameters () {
      var form = document.getElementById('search-form');
      var inputs = form.getElementsByTagName('input');
      var result = '';
      var i;
      for (i = 0; i < inputs.length; i++) {
        if (inputs[i].value) {
          result += "&" + inputs[i].name + "=" + inputs[i].value;
        }
      }
      return result;
    }
    

    In the onClick handler for your patient data links, you'll call this function and append its result to your query string:

    element.onclick = function () {
      var patientDataUrl = '/patients.php?param1=someValue';
      patientDataUrl += getQueryParameters();
      /* then do your ajax stuff as normal */
    };
    

    Then on the server side, within patients.php simply check for the presence of the search fields i.e.

    if(isset($_GET['patient-name'])) {
      $patient_name = mysql_real_escape_string($_GET['patient-name']);
      $query = "SELECT * FROM `patients` WHERE `patient_name`='$patient_name';";
    } else {
      $query = "SELECT * FROM `patients`;";
    }
    

    (make sure you sanitize the string!)

    I'd recommend considering a JS framework to make your life much easier (for instance, jQuery would allow you to send this via POST or easily serialize it into a GET query string via .serialize())

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程