2014-10-17 97 views
0

我正在處理的項目基本上涉及輸入框並將它們存儲到mysql數據庫中。我試圖使用JavaScript來生成此表單,如果需要可以添加另一個表單。我想我遇到的問題是讓它爲第二個生成的表單分配一個不同的「名稱」。 ex紅色,red1,red2等。然後也試圖讓這個與php-mysqli一起工作。多次生成相同的輸入

到目前爲止,我已經能夠將它發佈到表格中,但它將值發佈在第二組輸入框中。 有人可以提供一些建議嗎?

<form id="color_form" action="postcolors.php" method="post"> 
 

 
<input id="name" class="color_entry" action="postcolors.php" method="post" name="name" placeholder="song name" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAA…nt: scroll; background-position: right center; cursor: auto;"></input> 
 
<input id="red" class="color_entry" action="" method="post" name="red" placeholder="red"></input> 
 
<input id="green" class="color_entry" action="" method="post" name="green" placeholder="green"></input> 
 
<input id="blue" class="color_entry" action="" method="post" name="blue" placeholder="blue"></input> 
 
<input id="color" class="color_entry" action="" method="post" name="color" placeholder="color"></input> 
 
<input id="name" class="color_entry" action="postcolors.php" method="post" name="name" placeholder="song name"></input> 
 
<input id="red" class="color_entry" action="" method="post" name="red" placeholder="red"></input> 
 
<input id="green" class="color_entry" action="" method="post" name="green" placeholder="green"></input> 
 
<input id="blue" class="color_entry" action="" method="post" name="blue" placeholder="blue"></input> 
 
<input id="color" class="color_entry" action="" method="post" name="color" placeholder="color"></input> 
 
<input type="submit" value="submit" action="postcolors.php" method="post"></input> 
 

 
</form>
$song=mysqli_real_escape_string($connect, $_POST['name']); 
 
$song = str_replace(' ', '', $song); 
 
mysqli_query($connect, "CREATE TABLE $song (id int(4) NOT NULL auto_increment, red int(2) NOT NULL, green int(2) NOT NULL, blue int(2) NOT NULL, color varchar(30) NOT NULL, index(id))"); 
 

 
$red=mysqli_real_escape_string($connect, $_POST['red']); 
 
$green=mysqli_real_escape_string($connect, $_POST['green']); 
 
$blue=mysqli_real_escape_string($connect, $_POST['blue']); 
 
$color=mysqli_real_escape_string($connect, $_POST['color']); 
 

 

 
mysqli_query($connect, "INSERT INTO $song (red,green,blue,color) VALUES ('$red', '$green', '$blue', '$color')");

+0

您不能重複ID,ID =「名稱」只能在頁面中出現一次。 – Fahad 2014-10-17 11:38:15

回答

1

首先id=""必須在頁面上是唯一的,所以除非您實際上在JavaScript中使用id's,否則您可以將它們關閉。如果你在JavaScript中使用它們,你將不得不想辦法使它們獨一無二。

例如,您可以通過使用name="color[]"來讓瀏覽器將字段名稱作爲數組返回而不是標量值。

所以更改名稱字段像這樣: -

<form id="color_form" action="postcolors.php" method="post"> 

<input class="color_entry" action="postcolors.php" method="post" name="name[]" placeholder="song name" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAA…nt: scroll; background-position: right center; cursor: auto;"></input> 
<input class="color_entry" action="" method="post" name="red[]" placeholder="red"></input> 
<input class="color_entry" action="" method="post" name="green[]" placeholder="green"></input> 
<input class="color_entry" action="" method="post" name="blue[]" placeholder="blue"></input> 
<input class="color_entry" action="" method="post" name="color[]" placeholder="color"></input> 
<input class="color_entry" action="postcolors.php" method="post" name="name[]" placeholder="song name"></input> 
<input class="color_entry" action="" method="post" name="red[]" placeholder="red"></input> 
<input class="color_entry" action="" method="post" name="green[]" placeholder="green"></input> 
<input class="color_entry" action="" method="post" name="blue[]" placeholder="blue"></input> 
<input class="color_entry" action="" method="post" name="color[]" placeholder="color"></input> 
<input type="submit" value="submit" action="postcolors.php" method="post"></input> 

</form> 

現在$ _ POST將有$ _ POST [ '顏色'] [0],$ _ POST [ '顏色'] [1]例如使您可以將處理更改爲類似於

foreach ($_POST['name'] as $idx => $name) { 

    mysqli_query($connect, 
       "INSERT INTO $song (red,green,blue,color) 
        VALUES ('{$_POST['red'][$idx']}', 
          '{$_POST['green'][$idx']}', 
          '{$_POST['blue'][$idx']}', 
          '{$_POST['color'][$idx']}' 
         )" 
       ); 
} 
+0

所以按名稱引用引用,而不是ID?我沒有意識到這一點。這看起來可能是我想要做的。 – 2014-10-17 17:03:19

+0

「id」與PHP代碼完全無關,只有在編碼javascript時才具有相關性。 – RiggsFolly 2014-10-17 18:34:20

+0

我使用javascript生成表單,所以它仍然是相關的權利? – 2014-10-17 21:41:22

0

IIRC一個可以使用紅色[]作爲盒子的名字,這將使$ _ POST [ '紅']數組遍歷。

相關問題