如何从插入动态添加行到数据库值 [英] How to insert values from dynamically added rows into database

查看:402
本文介绍了如何从插入动态添加行到数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直停留在这几天。我真的AP preciate你的帮助。

I've been stuck on this for days. I really appreciate your help.


  • 我的工作产生动态添加行当PHP形式
    用户点击按钮添加

  • 要保存值到数据库中,用户必须点击提交
    按钮。

  • 是我现在面临的问题是,该数据库不保存
    从动态行的值。

AUTOCOMPLETE&安培;动态ROWS

<script>
//autocomplete
  $(function() {
    function log( message ) {
      $( "<div>" ).text( message ).prependTo( "#log" );
      $( "#log" ).scrollTop( 0 );
    }

    $( "#ItemName" ).autocomplete({
      source: "user/requisition_search.php",
      minLength: 1,
      select: function( event, ui ) 
          {
              $('#ItmId').val(ui.item.id);
              $('#StkId').val(ui.item.stkId);
              $('#ItmNameDis').val(ui.item.value);
              $('#ItmUOM').val(ui.item.uom);
              $('#ItmQty').val(ui.item.qty);

          }

    });
  });
//dynamic rows
    $(document).ready(function(){

        $("#add").on("click",function(){

          var rowcount = $("#rowcount").val();
          var row = 
          '<tr id="'+rowcount+'"><td>'+$("#ItmId").val()+'</td><td><input readonly="readonly" name="StkId[]" value="'+$("#StkId").val()+'"/></td><td>'+$("#ItemName").val()+'</td><td>'+$("#ItmUOM").val()+'</td><td>'+$("#ItmQty").val()+'</td><td><input readonly="readonly" name="ReqQty[]" value="'+$("#ReqQty").val()+'"/></td></tr>';

           rowcount = parseInt(rowcount)+1;

           $("#rowcount").val(rowcount);
           $("#dataTab").append(row);
           $("#dataTab").show();
           $("#submit").show();           

        });

    });
</script>

HTML

<form name="jqtest" action="submit.php" method="post">
<label for="ItemName">Search : </label>
<input id="ItemName" size="50"/>

<label for="ItmId">Item Id </label>
<input name="ItmId" id="ItmId" readonly="readonly"/>

<label for="StkId">Stock Id </label></td>
<input name="StkId" id="StkId" readonly="readonly"/>

<label for="ItmNameDis">Item Name </label>
<input name="ItmNameDis" id="ItmNameDis" size="50" readonly="readonly"/></td>

<label for="ItmUOM">Unit Of Measurement </label>
<input name="ItmUOM" id="ItmUOM" readonly="readonly"/>

<td><label for="ItmQty">Quantity Available </label>
<input name="ItmQty" id="ItmQty" readonly="readonly"/>

<label for="ReqQty">Quantity </label>
<input name="ReqQty" id="ReqQty" onkeypress="return numbersOnly(event)" onkeyup="ItmQty_Availability()" disabled="disabled"/>

      <p>
        <input type="reset" name="reset" id="reset" value="RESET"/>
        &nbsp;&nbsp;
        <input type="button" name="add" id="add" value="ADD"/>
      </p>

      <input type="hidden" name="rowcount" id="rowcount" value="1"/>

<table id="dataTab" width="90%" style="display:none;" border="1" cellpadding="0" cellspacing="0">
        <tr>
          <th>Item ID</th>
          <th>Stock ID</th>
          <th>Item name</th>
          <th>UOM</th>
          <th>Quantity Available</th>
          <th>Quantity Requested</th>
        </tr>
      </table>
      <p>&nbsp;</p>
      <p><input style="display:none;" type="submit" name="submit" id="submit" value="SUBMIT"/></p>
    </form>

submit.php

<?php

$num =  $_POST['rowcount'];
for($i=0;$i<$num;$i++)
{

      $strStkId = "";
      if(!empty($_POST)){
          if(isset($_POST["StkId[]"])){
              $strStkId = $_POST["StkId[]"];
          }else{
               echo "<font color=red>Enter the Stock Id</br></font>";
          }

      }
      $strReqQty = "";
      if(!empty($_POST)){
          if(isset($_POST["ReqQty[]"])){
              $strReqQty = $_POST["ReqQty[]"];
          }else{
               echo "<font color=red>Enter the Quantity</font>";
          }

      }

        $tsql =
        "INSERT INTO REQUISITION
        (RequestQuantity, StockId)
        VALUES
        ('$strReqQty','$strStkId')
        ";

        $result = sqlsrv_query($conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));   

            if (!$result) {
            die ('<script>
                window.alert("Please enter the requisition details !")
                window.location.href = "requisition.php"; </script>');
            }
            else 
            echo '<script>alert("Your Requisition is In Process"); </script>';
            sqlsrv_close($conn);
}
?>

更新时间:

我已经学会了 NAME =StkId NAME =StkId []在冲突动态行和输入。
所以我改变弄成这个样子:

I've learnt that name="StkId" and name="StkId[]" clashes in the dynamic rows and the inputs. So i've change into something like this :

var row = '<tr id="'+rowcount+'"><td>'+$("#ItmId").val()+'</td><td><input readonly="readonly" name="StkId2" value="'+$("#StkId").val()+'"/></td><td>'+$("#ItemName").val()+'</td><td>'+$("#ItmUOM").val()+'</td><td>'+$("#ItmQty").val()+'</td><td><input readonly="readonly" name="ReqQty2" value="'+$("#ReqQty").val()+'"/></td></tr>';

现在的数据插入到数据库中,但如果用户输入多个项,仅有一个数据插入到数据库中。我做错了什么?我猜想它在 submit.php 部分,但我想不出为什么。

Now the data are inserted into the database but IF the user enters more than one item, only one data is inserted into the database. What did i do wrong? I guess its at the submit.php part but i can't figure out why.

推荐答案

您不会得到动态的 $生成的行值_ POST 上提交表单,因为这些都不是在所有的表单输入,但只显示为表中的一行。但是你可以实现与jQuery的AJAX的帮助你的功能。在点击提交按钮,调用你的 submit.php 使用AJAX。对于如何检索那些动态行的值的样品溶液在下面给出。您可以适应您的要求这个方法。

You won't get dynamically generated row values in $_POST on submitting the form because those are not at all form inputs but just displaying as row in a table. But you can achieve your functionality with the help of jQuery AJAX. On clicking the submit button, calls your submit.php using AJAX. The sample solution for how to retrieve values of those dynamically rows is given below. You can adapt this method for your requirement.

主页。

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>jQuery Dynamic Rows</title>
    <meta charset="utf-8">
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function(){

        $("#add").on("click",function(){

          var rowcount = $("#rowcount").val();
          var row = '<tr class="dynamic" id="'+rowcount+'"><td>'+$("#itemid").val()+'</td><td>'+$("#itemname").val()+'</td><td>'+$("#uom").val()+'</td><td>'+$("#quantity").val()+'</td></tr>'; 

           rowcount = parseInt(rowcount)+1;

           $("#rowcount").val(rowcount);
           $("#dataTab").append(row);
           $("#dataTab").show();
           $("#submit").show();            

        });

        $("#submit").on("click",function(){
                alert("submit");
                var jsonObj = [];
                i=0;
            $("#dataTab tr.dynamic").each(function(){

               var td = $(this).find('td');

                itemId = td.eq(0).text();
                itemName = td.eq(1).text();
                uom = td.eq(2).text();
                quantity = td.eq(3).text();

                jsonObj.push({
                    itemId: itemId,
                    itemName: itemName,
                    uom: uom,
                    quantity: quantity,
                });
            });

            var dataString = JSON.stringify(jsonObj);


                $.ajax({
                    url: "submit.php",
                    type: "POST",
                    data: {json:dataString},
                    success: function(response){
                               alert(response);
                             }
                });

        });

    });

</script>
    </head>

    <body>


     <form name="jqtest" action="#">

       Item ID : <input type="text" name="itemid" id="itemid"/><br/><br/>
       Item name : <input type="text" name="itemname" id="itemname"/><br/><br/>
       UOM : <input type="text" name="uom" id="uom"/><br/><br/>
       Quantity : <input type="text" name="quantity" id="quantity"/><br/><br/>

       <p> <input type="reset" name="reset" id="reset" value="RESET"/>&nbsp;&nbsp;<input type="button" name="add" id="add" value="ADD"/> </p>
        <input type="hidden" name="rowcount" id="rowcount" value="1"/>
     </form>

     <table id="dataTab" style="display:none;" border="1">

      <tr>
        <th>Item ID</th>
        <th>Item name</th>
        <th>UOM</th>
        <th>Quantity</th>
      </tr>

     </table>

    <p> <input style="display:none;" type="button" name="submit" id="submit" value="submit"/> </p>

    </body>

    </html>

submit.php

submit.php

<?php

  $arr = json_decode($_POST['json']);


  for($i=0; $i<count($arr); $i++)
   {
     echo "Row ".$i."\n";
     echo "itemId > ".$arr[$i]->itemId.", itemName > ".$arr[$i]->itemName.", uom > ".$arr[$i]->uom.", quantity > ".$arr[$i]->quantity."\n";

   }

?>  

这篇关于如何从插入动态添加行到数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆