2012-02-16 6 views
0

"+"또는 "-"버튼을 클릭하면 양식에 입력 (텍스트 상자)을 추가하거나 제거 할 수있는 양식을 만들려고합니다.PHP를 사용하여 동적으로 텍스트 상자 추가하기

지금은 하나의 상자 만 추가하고 제거 할 수 있지만 그 이상은 추가 할 수 없습니다.

편집 - GET을 사용하여 작업하게되었습니다. 당신이 관심이 있다면 내가 한 일입니다.

<? 
$j=1; //sets the value initially when the page is first loaded 

if($_GET['num'] >= 1){ 
    $j = mysql_real_escape_string($_GET['num'])+1; 
} 

//displays the text boxes 
echo '<table>'; 
for($i = 0; $i<$j; $i++){ 
    echo '<tr><td><input type="textbox" name="input[]"></td></tr>'; 
} 
    //displays the + and - buttons to add or remove text boxes 
$a = $j-2; 

echo '</table>'; 
echo '<a href="helplist.php?num='.$j++.' "> + </a>'; 
echo '<a href="helplist.php?num=' .$a. '"> - </a>'; 
?> 
+0

나는이 문제가 실제로 무엇인지 이해하지 못한다. $ j의 가치는 무엇입니까? –

+0

내 코드를 편집했습니다. 미안합니다. 페이지로드시마다 – user1104854

+0

이 전송되고, j는 1입니다. 당신은 상태를 저장하거나 js fort를 사용하면된다. (어쨌든 훨씬 쉬워진다) –

답변

3

숨겨진 양식 필드에 $ j의 값을 저장해야합니다.

예 : <input type=hidden value=$j name=j>

<? 
if(!isset($_POST['j'])){ 
    static $j=1; 
} 
else{ 
//j is a reference for $i in the loop. $i will loop while it is less than $j. 

if(isset($_POST['plus'])){ 
    $j = $_POST['j']+1; //by incrementing $j, $i will loop one more time. 
} 

if(isset($_POST['minus'])){ 
    if($j < 1){ //if there is only one box the user can't remove it 
     $j = $_POST['j']-1; 
    } 
} 
} 
//displays the text boxes 
echo '<form method="post"> 
<input type="hidden" name="j" value="' . $j . '"> 
<table>'; 
for($i = 0; $i<$j; $i++){ 
    echo '<tr><td><input type="textbox" name="input[]"></td></tr>'; 
    echo 'i='.$i.'<br/>'; //checking value of $i 
    echo 'j='.$j.'<br/>'; //checking value of $j 
} 
    //displays the + and - buttons to add or remove text boxes 
echo '<tr><input type ="submit" value="+" name="plus"> 
     <input type ="submit" value="-" name="minus"></tr></table></form>'; 
?> 

난 단지 당신에게 뒤에 아이디어를 보여주기 위해이 테스트하지 않았다.

+0

증가 시키거나 그것을 정적으로 설정하는 것은 어떻습니까? – user1104854

+0

답안을 수정하겠습니다. –

+0

이것은 루프 또는 다른 곳으로 이동합니까? – user1104854

관련 문제