Alternamaton Posted July 15, 2007 Share Posted July 15, 2007 This isn't the actual script I'm using, it's just an example of the functionality that I'm going for. <HTML> <BODY> <?php $counter = 2; echo "<FORM method = 'post'> Enter a number: <input type = 'text' name = 'number[1]' size = 5> <br>"; while (isset($_POST['add_number'])) { echo "<br>Enter another number: <input type = 'text' name = 'number[" . $counter . "]' size = 5>"; $counter++; unset($_POST['add_number']); } ?> <br> <input type = 'submit' name = 'add_number' value = 'Another number!'> </FORM> </BODY> </HTML> If you click the 'Another number!' button once, the script echoes another input field. However, if you continue clicking the button after that, nothing happens. It should ( "should"... ) echo another input field every time you click the button, because every time you click it the variable $_POST['add_number'] is first set and then unset (and therefore ready to be set again, at least in theory...). I tried it with "if" instead of "while" (though in this case I don't really see the difference), but that didn't work either. What gives? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 15, 2007 Share Posted July 15, 2007 what is this script suppose to do? are you adding form inputs? or numbers? i'm confused Quote Link to comment Share on other sites More sharing options...
trq Posted July 15, 2007 Share Posted July 15, 2007 What gives? You'll need to store $counter in a session, otherwise, its simply rest back to 2 on each request. Quote Link to comment Share on other sites More sharing options...
pedrobcabral Posted July 15, 2007 Share Posted July 15, 2007 Don't know if the POST is a must-have, once you could either use vars on URL using GET. Quote Link to comment Share on other sites More sharing options...
trq Posted July 15, 2007 Share Posted July 15, 2007 <?php session_start(); ?> <HTML> <BODY> <?php $_SESSION['counter'] ? isset($_SESSION['counter']) : $_SESSION['counter'] = 2; echo "<FORM method = 'post'> Enter a number: <input type = 'text' name = 'number[1]' size = 5> <br>"; while (isset($_POST['add_number'])) { echo "<br>Enter another number: <input type = 'text' name = 'number[" . $_SESSION['counter'] . "]' size = 5>"; $_SESSION['counter']++; unset($_POST['add_number']); } ?> <br> <input type = 'submit' name = 'add_number' value = 'Another number!'> </FORM> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
Alternamaton Posted July 15, 2007 Author Share Posted July 15, 2007 Edit: Updated to give the additional form inputs the correct names ("name = number[" . $_SESSION['counter'] . "...", not "name = " . $counter . "..."). Still doesn't work. That's good to know, but even if every form input after the fist has the same name ('number[2]'), they should still show up every time you hit the submit button, shouldn't they? In other words, thank you for pointing that out, but that's not the main problem with the script. The main problem is that after you click the submit button the first time, nothing happens on subsequent clicks. Here's the updated version of the code, with a bit at the end to see if clicking the submit button adds to the $_SESSION['counter'] variable. <?php session_start(); $_SESSION['counter'] = 2; ?> <HTML> <BODY> <?php echo "<FORM method = 'post'> Enter a number: <input type = 'text' name = 'number[1]' size = 5> <br>"; while (isset($_POST['add_number'])) { echo "<br>Enter another number: <input type = 'text' name = 'number[" . $_SESSION['counter']. "]' size = 5>"; $_SESSION['counter']++; unset($_POST['add_number']); } ?> <br> <input type = 'submit' name = 'add_number' value = 'Another number!'> </FORM> <?php echo $_SESSION['counter']; ?> </BODY> </HTML> It still doesn't work, and no matter how many times you click the submit button $_SESSION['counter'] is echoed as 3. Quote Link to comment Share on other sites More sharing options...
trq Posted July 15, 2007 Share Posted July 15, 2007 Sorry, theres actually quite a bit more to it then that. I didn't really read your code. The while has got to go.... variables do not persist. You also left out a chunk of my code (which id'e foobared anywho). <?php session_start(); isset($_SESSION['counter'] ? $_SESSION['counter'] : $_SESSION['counter'] = 2; ?> <HTML> <BODY> <?php echo "<FORM method = 'post'> Enter a number: <input type = 'text' name = 'number[1]' size = 5> <br>"; $_SESSION['form'] = ''; if (isset($_POST['add_number'])) { $_SESSION['form'] .= "<br>Enter another number: <input type = 'text' name = 'number[" . $_SESSION['counter'] . "]' size = 5>"; $_SESSION['counter']++; } ?> echo $_SESSION['form']; <br> <input type = 'submit' name = 'add_number' value = 'Another number!'> </FORM> <?php echo $_SESSION['counter']; ?> </BODY> </HTML> You know this type of thing is much more efficiently done in Javascript? Quote Link to comment Share on other sites More sharing options...
Alternamaton Posted July 15, 2007 Author Share Posted July 15, 2007 $_SESSION['form'] = ''; if (isset($_POST['add_number'])) { $_SESSION['form'] .= "<br>Enter another number: <input type = 'text' name = 'number[" . $_SESSION['counter'] . "]' size = 5>"; $_SESSION['counter']++; } ?> echo $_SESSION['form']; So you store the HTML code for the form as a $_SESSION variable, and then when the user clicks "Another number!" the $_SESSION variable is simply appended, and then the whole form is echoed. I take it that this is because every time the user clicks a submit button the page is refereshed, and the script doesn't remember that the LAST time the submit button was clicked, the form had ALREADY been added to. D'oh. I think I understand now. Thank you! Oh, and I don't know Javascript, but I will look into learning it. I am going to be taking the input variables and feeding them to MySQL, though. Would JavaScript be able to handle that? Quote Link to comment Share on other sites More sharing options...
ss32 Posted July 15, 2007 Share Posted July 15, 2007 it would be MUCH easier and more practical to do this with a javascript thing... and the fact that you dont have to store a session variables or the values of the temporary fields is a plus. if you werent thinking about storing temporary data... it would be an inconvenience for the user to have to fill in every number again. <script language="javascript"> function add_input(id) { box = document.getElementById("newForm_" + id); box.innerHTML = 'insert your form data here'; box.innerHTML += '<div id="newForm_'+(id + 1)+'"></div>'; //i dont exactly remember how to set this property, so the following line is pseudo-pseudocode document.getElementById("add_number").onmouseup = "add_input(" + (id+1) + ")"; } </script> of course, that requires that you add a div at the end of your form, and also that you add an onmouseup event to trigger that function. its just the way i would do it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.