2014-10-17 8 views
0

기본적으로 작업중인 프로젝트는 입력 상자를 포함하고이를 MySQL 데이터베이스에 저장하는 작업을 포함합니다. 필요하다면 다른 폼을 추가 할 수있는 폼을 생성하기 위해 자바 스크립트를 사용하려고합니다. 내가 겪었던 문제는 두 번째로 생성 된 폼에 다른 "이름"을 할당하는 것입니다. ex red, red1, red2, etc. 그런 다음 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 = "name"은 페이지에서 한 번만 발생할 수 있습니다. – Fahad

답변

1

처음에는 id=""이 페이지에서 고유해야합니다. 실제로 id's을 자바 스크립트에서 사용하지 않으면 제외 할 수 있습니다. 당신이 자바 스크립트에서 그들을 사용하는 경우 당신은 그들을 독특하게 만드는 방법을 생각해야합니다.

두 번째로 예를 들어 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가 아닌 이름으로 참조를 게시 하시겠습니까? 나는 그 사실을 몰랐다. 이게 내가하려고하는 것처럼 보일거야. –

+0

'id'는 PHP 코드와 전혀 관련이 없습니다. 자바 스크립트를 코딩하는 경우에만 관련성이 있습니다. – RiggsFolly

+0

javascript를 사용하여 양식을 생성하므로 여전히 관련성이 있습니까? –

0

IIRC 하나는 반복하는 $ _POST [ '빨간색'] 배열을 만들 것입니다 상자의 이름으로 [] 붉은 사용할 수 있습니다.

관련 문제