0

I'm trying to make a form that the allows the user to add or remove inputs (text box) to a form if they click a "+" or "-" button.

Right now it will only let me add one box (and also remove it), but I can't add any more than that.

EDIT - I got it to work using GET. Here's what I did if you're interested.

<?
$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>';
?>  
6
  • I don't really understand what the problem is. What is the value of $j? Commented Feb 16, 2012 at 2:37
  • I edited my code. Sorry about that. Commented Feb 16, 2012 at 2:39
  • on every submit, page load, j is 1 again. you have to store the state or use js fort this (much easier anyway) Commented Feb 16, 2012 at 2:39
  • When I echo $j it shows it as 2. Is there any way I can pass this value to another function which it reloads at the beginning? I don't know any js, I plan on learning it eventually. I'm currently working on a php project so I don't want to go out of my way at the moment. Commented Feb 16, 2012 at 2:44
  • now is a great time to start learning Commented Feb 16, 2012 at 2:47

1 Answer 1

3

You need to store the value of $j in a hidden form field.

example: <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>';   
?>  

I didn't test this out, only to show you the idea behind it.

Sign up to request clarification or add additional context in comments.

3 Comments

What about incrementing that or setting it to static?
Would this go in the loop or somewhere else?
Thanks for the help, I'll give yours a try sometime. I ended up just passing the value through "GET" instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.