簡體   English   中英

通過via表單提交html表數據(post)

[英]Submit html table data with via form (post)

我想在提交按鈕單擊上發布此html表。

01 - 我想在php代碼(服務器端)02中檢索此表的數據 - 我還想在另一頁上顯示該表。

我正在使用

PHP,JQuery

我有表格標簽中有很多行的html表。

<form id="frm_test" class="form-vertical" method="post">
  <table id="mytable">
    <tr>
      <td>
        <input type="text"  name="color_1" value="" />
      </td>
      <td>
        <input type="text"  name="color_2" value="" />
      </td>
      <td>
        <input type="text"  name="color_3" value="" />
      </td>
      <td>
        <input type="text"  name="color_4" value="" />
      </td>
    </tr>
    ......
    ......
    ......
    ......
    .....
  </table>

  <input type="submit"  name="submit" value="submit" />
</form>

===========

每行有4個輸入字段(每行4個單元格)。 和表中的一百行。 因此,如果我通過PHP代碼中的名稱獲取值,那么我必須編寫大量代碼以獲得100(行)* 4(輸入字段)= 400輸入。 所以我的問題是“實現這一目標的最佳途徑是什么”

由於您嘗試向后端提交具有相同“name”屬性的多行輸入/選擇,因此您需要在這些輸入(表行)中的名稱值末尾添加方括號[]。

<form>
<input type="number" name="examplevar" />
<table>
  <tr>
    <td><input type="text" name="color_1[]" /></td>
    <td><input type="text" name="color_2[]" /></td>
  </tr>
  <tr>
    <td><input type="text" name="color_1[]" /></td>
    <td><input type="text" name="color_2[]" /></td>
  </tr>
<table>
<input type="submit" />
</form>

這告訴瀏覽器為該name屬性創建一個數組。

在php中,將其讀作$_POST['color_1'][0]$_POST['color_2'][0] ,並根據需要循環播放。

括號在Phil Bailey的答案中,但沒有指出。 (添加因為最近的搜索引導我這個)

要發布表單,您需要添加action標記,該標記用於表示提交表單時要轉到的路徑

<form id="frm_test" class="form-vertical" name="THE_PHP_FILE_TO_POST.php" method="post">

如果要POST值,應指定具有特定name input字段。 如果您只希望表格可見,則應使用hidden type以便表單將POST數據,但輸入不可見。

<tr>
    <td>
        My value
        <input type="hidden" name="myValue" value="My value" />
    </td>
</tr>

發布表單后,您可以捕獲該PHP文件中的POST數據,如下所示:

//THE_PHP_FILE_TO_POST.php

if(isset($_POST))
{
    $myValue = $_POST['myValue']; //Contains the string "My value"
    //Do something with your POST
}

編輯獲取所有表格數據

if(isset($_POST))
{
    foreach($_POST as $inputName => $inputValue)
    {
        echo $inputName; //This is the name of an input field
        echo $inputValue; //This is the value of the input field 

    }
}

對於表輸入字段,PHP有一個奇怪的命名約定,您需要將該文件的名稱指定為數組。 對於以下使用PDO的SQL語句的一種處理方法。

select id,color_1,color_2,color_3,color_4 from colors;
<?php
// Get data
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_NUM);
reset($results);
echo '<table>';
while (list(, $i = each($results)) {
  echo  '<tr><td><input type=hidden name="id[]" value="' . $i[0] . '"></td>';
  echo '<td><input type=text name="color_1[]" value="'  . $i[1] . '"></td>';
  echo '<td><input type=text name="color_2[]" value="'  . $i[2] . '"></td>';
  echo '<td><input type=text name="color_3[]" value="'  . $i[3] . '"></td>';
  echo '<td><input type=text name="color_4[]" value="'  . $i[4] . '"></td>';
  echo '</tr>';
}
echo '</table>
?>

4個記錄的簡化$ _POST print_r輸出:

Array ( [[id] => Array ( [0] => 20 [1] => 21 [2] => 44 [3] => 45 ) 
[color_1] => Array ( [0] =>red [1] =>green [2] =>yellow [3] =>blue ) 
[color_2] => Array ( [0] =>purple [1] =>orange [2] =>green [3] =>red ) 
[color_3] => Array ( [0] =>red [1] =>green [2] =>yellow [3] =>blue ) 
[color_4] => Array ( [0] =>purple [1] =>orange [2] =>green [3] =>red ) )

您需要在表中添加輸入字段以發布數據。 喜歡

<input type="text"  name="fieldname" value="" />

如果您不想顯示字段,則可以使用type="hidden"字段

在表單中添加操作屬性。

“操作”屬性指示將在其上發布數據的網址。

暫無
暫無

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

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