显示相应行中ajax的返回值

时间:2015-05-12 05:56:29

标签: javascript php jquery html ajax

我需要在表上添加运行时行,然后将数据发送到数据库。我设法完成了大部分工作但是我被困在程序的两个部分上。

  1. 我正在使用Ajax检查下拉列表中的可用数量,我希望在相应的行中显示,但是不能,到目前为止我能够提醒正确的值。)

  2. 如果我填写任何行,php帖子会向我发送一个小于表单上可用参数的参数,但如果我添加行并且不填写任何信息,请向我发送正确数量的参数。 (即如果我添加3个空白行并单击提交,则提交4个值, - >因为一行是隐藏的,但是如果我填充屏幕中的任何3行,它只作为参数发送3个帖子为$ name)

     

  3. 
    
    function getSpQty(strURL) {
      var xmlhttp;
      console.log(this)
      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) {
    
          if (xmlhttp.status == 200) {
            alert(xmlhttp.responseText);
    
            document.getElementById('sp').value = xmlhttp.responseText;
    
          } else {
            alert("There was a problem while using XMLHTTP:\n" + xmlhttp.statusText);
          }
        }
      }
      xmlhttp.open("GET", strURL, true);
      xmlhttp.send(null);
    }
    
    <?php include_once( "dbConn-SP.php"); $sqlSP="select * from dbo.SparepartManagement" ; $stmtSP=s qlsrv_query($connIgms, $sqlSP); ?>
    <html>
    
    <head>
      <script src="js/jquery1.7.js"></script>
      <script>
        $(document).ready(function() {
          var id = 0;
          // Add button functionality
          $("table.dynatable button.add").click(function() {
            id++;
            var master = $(this).parents("table.dynatable");
            // Get a new row based on the prototype row
            var prot = master.find(".prototype").clone();
            prot.attr("class", "")
            prot.find(".id").attr("value", id);
            master.find("tbody").append(prot);
          });
          // Remove button functionality
          $("table.dynatable button.remove").live("click", function() {
            $(this).parents("tr").remove();
          });
        });
      </script>
      <style>
        .dynatable {
          border: solid 1px #000;
          border-collapse: collapse;
        }
        .dynatable th,
        .dynatable td {
          border: solid 1px #000;
          padding: 2px 10px;
          width: 170px;
          text-align: center;
        }
        .dynatable .prototype {
          display: none;
        }
      </style>
      <script type="text/javascript" language="JavaScript1.2" src="spQty.js"></script>
    </head>
    
    <body>
      <form action="test.php" method="post" name="frm">
        <table width="100%" class="dynatable">
          <thead>
            <tr>
              <th width="144">ID</th>
              <th width="144">Spareparts</th>
              <th width="144">Quanitity</th>
              <th width="144">Issued Quantity</th>
              <th width="140">
                <button type="button" class="add">Add</button>
              </th>
            </tr>
          </thead>
          <tbody>
            <tr class="prototype">
              <td>
                <input type="text" name="id[]" value="0" class="id" />
              </td>
    
              <td>
                <select style="width:200px" name="name[]" id="sp" onChange="getSpQty('SPName.php?spareparts='+this.value)">
                  <option value="error">Select Spare Parts</option>
                  <?php while($viewAllSP=s qlsrv_fetch_array($stmtSP,SQLSRV_FETCH_ASSOC)) { echo "<option value='$viewAllSP[GeneralItemDescription]'>$viewAllSP[GeneralItemDescription]</option>\n"; }; ?>
                </select>
              </td>
              <td>
                <input type="text" name="col4[]" value="" />
              </td>
              <td>
                <input type="text" name="col3[]" value="" />
              </td>
              <td>
                <button class="remove">Remove</button>
              </td>
            </tr>
        </table>
        <input type="submit" value='send' />
      </form>
    </body>
    
    </html>
    &#13;
    &#13;
    &#13;

    这是SPName.php      

    include_once("dbConn-SP.php");
    
    $spareparts=$_REQUEST['spareparts'];
    
    
    $tsqls = "select InitialQty from SparepartManagement where GeneralItemDescription ='$spareparts'";
    
    
    
    
        $params = array();
        $options =  array("Scrollable" => SQLSRV_CURSOR_KEYSET );
        $stmt = sqlsrv_query($connIgms, $tsqls , $params, $options);
    
        $row_count = sqlsrv_num_rows($stmt);
    
    
    
    
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
                   $test = $row['InitialQty'];
    
    
                }
    
    
    
    echo $test; 
    
    ?>
    

    这里是test.php

    <?PHP
    error_reporting(0);
    
    include_once("dbConn-SP.php");
    
    $Qty = $_POST["col4"];
    $IssuedQty = $_POST["col3"];
    $name= $_POST["name"];
    //Step 3: Insert statment
    print_r($name);
    for ($i = 1; $i < sizeof($Qty); $i++) {     
                $j=$i-1;
    $Insert = "INSERT INTO SparepartTransaction (SPDesc,InitialQty,QtyIssued) Values ".
                " ('$name[$j]','$Qty[$i]','$IssuedQty[$i]')";   
    $stmt = sqlsrv_query($connIgms,$Insert);            
    
                }
    sqlsrv_close($connIgms);
    ?> 
    

    遗憾的是,我没有足够的声誉在这里发布图片,所以这里是我屏幕上实际显示的链接 http://prntscr.com/749n4n

    我想要的是,而不是提醒 - &gt;警报(xmlhttp.responseText); 我希望这个值显示在下拉列表旁边的单元格上。任何帮助将不胜感激。

0 个答案:

没有答案