簡體   English   中英

PHP如何從動態生成的表單插入數據輸入

[英]PHP How to Insert Data Input from a Dynamically-Generated Form

在下一步將輸入的數據插入動態生成的表單中時,我需要幫助。 我在其他類似問題上找了好幾天,但仍然遺漏了一部分。

這是我的HTML表單(減去錯誤陷阱/驗證):

<form id="form" name="form" method="post" action="recipeaddsubmit.php">
<table id="myTable">
            <tr>
              <td width="10%">Amount</td>
              <td width="10%">Measure</td>
              <td width="35%">Ingredient</td>
            </tr>
            <tr>
              <td valign="top">
                <input type="text" name="Amt1" id="Amt1" size="1" />
                <input type="text" name="Amt2" id="Amt2" size="1"  />
              </td>
              <td valign="top">
                 <select name="Measure" id="Measure">
                   <option>Cup</option>
                   <option>Ounce</option>
                 </select>
              </td>
              <td valign="top">
                 <input type="text" name="Ing" id="Ing" size="40" />
              </td>
             </tr>
         </table>
         <button type="button" onclick="displayResult()">Insert new row</button>
         <input type="submit" name="button" id="button" value="Submit" />
         </form>

這是我標頭中的javascript:

<script>
function displayResult()
{
    var table=document.getElementById("myTable");
    var row=table.insertRow(-1);
    var cell1=row.insertCell(0);
    var cell2=row.insertCell(1);
    var cell3=row.insertCell(2);
            cell1.innerHTML="<input type='text' name='Amt1' id='Amt1' size='1' /> 
    <input type='text' name='Amt2' id='Amt2' size='1'  />";
    cell2.innerHTML="<select name='Measure' id='Measure'> <option>Cup</option> 
    <option>Ounce</option></select>";
    cell3.innerHTML="<input type='text' name='Ing' id='Ing' size='40' />";
}

</script>    

這是我的嘗試/部分mysqli插入語句(checkInput是我之前使用的自創建驗證函數,沒有問題):

$amt1 = checkInput($_POST['Amt1']);
$amt2 = checkInput($_POST['Amt2']);
$measure = checkInput($_POST['Measure']);
$ing = checkInput($_POST['Ing']);

$ingAddQuery ="INSERT INTO Ingredient (Amt1, Amt2, Measure, Ing)
VALUES ({$amt1}, {$amt2}, {$measure}, {$ing})";

mysqli_query($mysqli,$ingAddQuery);              

    if (!mysqli_query($mysqli,$ingAddQuery))
{
  die('Error: ' . mysqli_error());
}

我不明白的是如何在一定數量的行中增加一個foreach循環。 在這種情況下,我了解概念但不了解應用程序。

謝謝您的幫助!

我有個建議。

您為什么不創建隱藏的輸入類型。

<input type="hidden" name="cellCount" value="0" />

每次u插入新行時,請更改此隱藏字段的值。 這不會顯示在瀏覽器上。 當您提交表單時,此隱藏元素也將得到提交。 可以在服務器上通過$_POST["cellCount"]進行檢索。

希望這對你有幫助。

將名稱從amt1更改為amt1 []等。

所以

<input type="text" name="Amt1[]" id="Amt1[]" size="1" />

提交表單時,

$_POST['Amt1']; //is an array

將是一個數組,所以你可以做

$Amt1Array = $_POST['Amt1'];
foreach($Amt1Array => $Amt1){
//do stuff here
}

更好的是,您可以使用js索引數組以創建類似...的名稱

<input type="text" name="Amt1[0]" id="Amt1[0]" size="1" />
<input type="text" name="Amt2[0]" id="Amt2[0]" size="1" />

<input type="text" name="Amt1[1]" id="Amt1[1]" size="1" />
<input type="text" name="Amt2[1]" id="Amt2[1]" size="1" />

然后在PHP中,您可以

$Amt1Array = $_POST['Amt1'];
$Amt2Array = $_POST['Amt2'];

foreach($Amt1Array as $key => $Amt1Value){
$Amt2Value = $Amt2Array[$key];
//do stuff with rows
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM