根据xmlhttp.responseText

时间:2016-06-04 09:28:02

标签: javascript php html

目前我有一个JavaScript函数,当我知道将从我的PHP端返回的结果数量时(我之前准备我的HTML表,知道只返回一行作为示例),它一直很好用。 ,如下所示:

<script>
   function findInfo(str1, str2) {

       var searchOne (str1.value);
       var searchTwo = (str2.value);

       if (searchOne.length === 0 || searchTwo.length === 0) {
           document.getElementById("existingTableCell").innerHTML = "Missing mandatory field(s)!";                    
           return;
       } else {                    
           var xmlhttp = new XMLHttpRequest();
           xmlhttp.onreadystatechange = function ()
           {
               if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
               {                        
                   var splitResponse = xmlhttp.responseText.split(":",5);                                                        
                   var firstCell = splitResponse[0];                            
                   var secondCell = splitResponse[1];                            
                   var thirdCell = splitResponse[2];                           
                   var fourthCell = splitResponse[3];                            
                   var fifthCell = splitResponse[4];                            

                   document.getElementById("cellID1").innerHTML = firstCell;
                   document.getElementById("cellID2").innerHTML = secondCell;
                   document.getElementById("cellID3").innerHTML = thirdCell;
                   document.getElementById("cellID4").innerHTML = fourthCell;
                   document.getElementById("cellID5").innerHTML = fifthCell;

               }
           };

           xmlhttp.open("GET", "myPHPLogic.php?varA="+ searchOne + "&varB=" +  searchTwo, true);                    
           xmlhttp.send();                    
       }
   }
</script>

但现在看到好像在PHP端运行的MSSQL查询可能有不确定数量的行返回我不明白我如何继续使用这个xmlhttp.responseText.split方法和预先创建的表?

不确定什么是处理这个要求的最佳方法?当我尝试通过xmlhttp.responseText时,我是否在JavaScript函数中构建新行?

更新我无法绕过这种语法和逻辑,我已经尝试了几个小时了:(< / p>

<script>
function findInfo(str1, str2) {

    var searchOne (str1.value);
    var searchTwo = (str2.value);

    if (searchOne.length === 0 || searchTwo.length === 0) {
        document.getElementById("existingTableCell").innerHTML = "Missing mandatory field(s)!";                    
        return;
    } else {                    
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function ()
        {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
            {                        
                /* 
                   I HAVE A MSSQL RESPONSE COMING BACK FROM THE PHP THAT WOULD LOOK AS FOLLOWS:
                   Value: Value2: Value3: Value4: ETC: ETC:                 
                */

                var responseSplit = xmlhttp.responseText.split(":");                
                //Value, Value2, Value3, Value4, ETC, ETC,

                /*
                  I have a table that looks as follows:
                  Column 1   Column 2   Column 3

                  I want to insert //Value, Value2, Value3, Value4, ETC, ETC, all in their own cell in column 1..
                */              

                var arrayLength = responseSplit.length;                                                                   
                for (var i = 0; i < arrayLength; i++) {                                                                                         
                    $(tableOne).find(tableOneBody).find(tableOneTableRow1).append("<td>"+responseSplit[i]+"</td>");
                }
            };

            xmlhttp.open("GET", "myPHPLogic.php?varA="+ searchOne + "&varB=" +  searchTwo, true);                    
            xmlhttp.send();                    
        }
    }
</script>

1 个答案:

答案 0 :(得分:0)

我必须做出很多改动来解决这个问题,我将简要地尝试并提供一个有用的答案:

  1. 我从HTML表中删除了所有数据,剩下的就是:
  2.     <div class="section">
       <div class="container">
          <div class="row">
             <div class="col-md-12">
                <table class="table" id="tableOne">
                   <thead>
                      <tr>
                         <th>Column One</th>
                         <th>Column Two</th>
                         <th>Column Three</th>
                      </tr>
                   </thead>
                   <tbody>
                   </tbody>
                </table>
             </div>
          </div>
       </div>
    </div>
    
    <div class="section">
       <div class="container">
          <div class="row">
             <div class="col-md-12">
                <table class="table" id="tableTwo">
                   <thead>
                      <tr>
                         <th>Column One</th>
                         <th>Column Two</th>
                      </tr>
                   </thead>
                   <tbody>
                   </tbody>
                </table>
             </div>
          </div>
       </div>
    </div>
    
    1. 我的JavaScript函数修改如下:

      <script>
          function findInformation(str1, str2) {
      
              var inputBoxOne = (str1.value);
              var inputBoxTwo = (str2.value);
      
              if (inputBoxOne.length === 0 || inputBoxTwo.length === 0) {
                  alert("Missing mandatory field(s)!");
                  return;
                  } else {
      
                      //TABLE ONE LOGIC
                      var xmlhttpOne = new XMLHttpRequest();
                      xmlhttpOne.onreadystatechange = function ()
      
                      {
                          if (xmlhttpOne.readyState === 4 && xmlhttpOne.status === 200)
      
                          {
                              var thetableBodyPart1 = xmlhttpOne.responseText;                                                                
                              $(tableOne).append(thetableBodyPart1);                                                               
                          }
                      };
      
                      //PHP 1 (findInformation.php)
                      xmlhttpOne.open("GET", "findInformation.php?q=" + inputBoxOne + "&w=" + inputBoxTwo, true);
                      xmlhttpOne.send();
      
                      //TABLE TWO LOGIC
                      var xmlhttpTwo = new XMLHttpRequest();
                      xmlhttpTwo.onreadystatechange = function ()
      
                      {
                          if (xmlhttpTwo.readyState === 4 && xmlhttpTwo.status === 200)
      
                          {                                                           
                              var thetableBodyPart2 = xmlhttpTwo.responseText;                                                                
                              $(tableTwo).append(thetableBodyPart2);                                
                          }
                      };                        
      
                      //PHP 2 (findInformation2.php)
                      xmlhttpTwo.open("GET", "findInformation2.php?e=" + inputBoxOne + "&r=" + inputBoxTwo, true);
                      xmlhttpTwo.send();                   
                  }
              }        
      

    2. 3A。 PHP Logic处理如下表所示的表格数据:

      <?php
      
      $q = $_REQUEST["q"];
      $w = $_REQUEST["w"];
      
      if ($q !== "" && $w !== "") {
      
      //MSSQL SERVER CONNECTION
      $serverName = "127.0.0.1\INSTANCENAME,PORTNUMBER"; //serverName\instanceName
      $connectionInfo = array("Database" => "DBNAME", "UID" => "USERNAME", "PWD" => "PASSWORD");
      $conn = sqlsrv_connect($serverName, $connectionInfo);
      
      if ($conn) {
          //echo "Connection established.<br />";
      } else {
          echo "Connection could not be established.<br />";
          die(print_r(sqlsrv_errors(), true));
      }
      
      
          $queryForTable1 =               "SELECT THINGONE, THINGTWO, THINGTHREE
                                           FROM TABLE                                     
                                           WHERE SOMETHING = $q   
                                           AND SOMETHINGELSE = $w;";
      
          $stmtForTable1 = sqlsrv_query($conn, $queryForTable1);
      
                  while ($row = sqlsrv_fetch_array($stmtForTable1, SQLSRV_FETCH_ASSOC)) {
                      echo'<tr>'; 
                      echo'<td>'. $row['THINGONE']."</td>";
                      echo'<td>'. $row['THINGTWO'].'</td>';
                      echo'<td>'. $row['THINGTHREE'].'</td>';
                      echo'<tr>';
                  }
      }
      
      else {
      
          echo "Missing data input!";
      
      }
      

      3B。 PHP Logic处理如表2所示的表数据添加:

      <?php
      
      $e = $_REQUEST["e"];
      $r = $_REQUEST["r"];
      
      if ($e !== "" && $r !== "") {
      
      //MSSQL SERVER CONNECTION
      $serverName = "127.0.0.1\INSTANCENAME,PORTNUMBER"; //serverName\instanceName
      $connectionInfo = array("Database" => "DBNAME", "UID" => "USERNAME", "PWD" => "PASSWORD");
      $conn = sqlsrv_connect($serverName, $connectionInfo);
      
      if ($conn) {
          //echo "Connection established.<br />";
      } else {
          echo "Connection could not be established.<br />";
          die(print_r(sqlsrv_errors(), true));
      }
      
      
          $queryForTable2 =               "SELECT THINGFOUR, THINGFIVE
                                           FROM TABLE                                     
                                           WHERE SOMETHING = $e   
                                           AND SOMETHINGELSE = $r;";
      
          $stmtForTable2 = sqlsrv_query($conn, $queryForTable2);
      
                  while ($row2 = sqlsrv_fetch_array($stmtForTable2, SQLSRV_FETCH_ASSOC)) {
                      echo'<tr>'; 
                      echo'<td>'. $row2['THINGFOUR']."</td>";
                      echo'<td>'. $row2['THINGFIVE'].'</td>';
                      echo'<tr>';
                  }
      }
      
      else {
      
          echo "Missing data input!";
      
      }
      

      我真的希望将来帮助某人!