cbr_600rr Posted November 17, 2009 Share Posted November 17, 2009 I am using a form to get data from a user, currently I am only getting one value and placing it in a database as shown below. ------------------------------------------------------------- <form action="file01.php" method="post"> <br>Enter a color<br> <p>color: <input type="text" name="color" /><input type="submit" /></p> </form> <?php $colorvar = $_POST['color'];?> (then I place into database) ------------------------------------------------------------- Id like to take this to the next level and let the user keep entering in colors until they are done, and store each of those colors in the database in a unique position. What is the best way to go about this? Should I use a Do or While loop and loop the form? Id like to set it up so the user has 1 form box and 2 buttons (submit and add) where submit would end the loop and add would allow them to add another color. or just the submit button, and they can use submit to enter all their colors and when they enter '0' into the box and hit submit it ends. Quote Link to comment https://forums.phpfreaks.com/topic/181934-looping-a-form-based-on-how-many-inputs-user-makes/ Share on other sites More sharing options...
ToonMariner Posted November 18, 2009 Share Posted November 18, 2009 firstly make your input an array by adding '[]' to the input name like so... <form action="file01.php" method="post"> <br>Enter a color<br> <p>color: <input type="text" name="color[]" /> <br>Enter a color<br> <p>color: <input type="text" name="color[]" /> <br>Enter a color<br> <p>color: <input type="text" name="color[]" /> ..... // use the button to add these (you only need add the SAME mark up each time its clicked) <br>Enter a color<br> <p>color: <input type="text" name="color[]" /> <input type="submit" /></p> </form> Then you can be cleaver in your query to insert them into the database... $values = implode("'),('" , $_POST['color']); $insert ="INSERT INTO `colours_table` (`colour`) VALUES ('".$values."')"; // run query etc etc. not the best to achieve this in terms of php code but its quick and nasty i'm giving you here... Quote Link to comment https://forums.phpfreaks.com/topic/181934-looping-a-form-based-on-how-many-inputs-user-makes/#findComment-959684 Share on other sites More sharing options...
cbr_600rr Posted November 18, 2009 Author Share Posted November 18, 2009 firstly make your input an array by adding '[]' to the input name like so... ..... // use the button to add these (you only need add the SAME mark up each time its clicked) Thanks for the help,...although I am slighty unclear with what you mean by the statement above. But, yeah I would like to add one color to the array at a time. I am a little lost though as to how I build the loop to build the array. The forms do not seem to like being inclosed in php tags. thanks Quote Link to comment https://forums.phpfreaks.com/topic/181934-looping-a-form-based-on-how-many-inputs-user-makes/#findComment-959754 Share on other sites More sharing options...
ngreenwood6 Posted November 18, 2009 Share Posted November 18, 2009 If you are trying to have them add the colors real time(add color while on page) you are going to have to use javascript to hold the data and then you can post it. However, If you are looking for a solution with a fixed number of inputs you can do as toonmariner suggests with creating an array but I am not sure what he was doing in the post but you can do this: <?php if($_POST){ $color_one = $_POST['color'][0]; $color_two = $_POST['color'][1]; $query = "INSERT INTO table (color_one,color_two) VALUES('$color_one','$color_two')"; } Quote Link to comment https://forums.phpfreaks.com/topic/181934-looping-a-form-based-on-how-many-inputs-user-makes/#findComment-959758 Share on other sites More sharing options...
cbr_600rr Posted November 18, 2009 Author Share Posted November 18, 2009 yes, I am trying to add them in real time since I do not know if the user will enter 1 input, or 20. so php cannot do this without javascript? hmm I was afraid of that. Any idea on what I should search for? I have been searching terms such as 'iterative forms' , 'dynamic forms', 'variable length forms' etc... but haven't turned up much. I guess I'll start looking into a javascript solution If you are trying to have them add the colors real time(add color while on page) you are going to have to use javascript to hold the data and then you can post it. However, If you are looking for a solution with a fixed number of inputs you can do as toonmariner suggests with creating an array but I am not sure what he was doing in the post but you can do this: <?php if($_POST){ $color_one = $_POST['color'][0]; $color_two = $_POST['color'][1]; $query = "INSERT INTO table (color_one,color_two) VALUES('$color_one','$color_two')"; } Quote Link to comment https://forums.phpfreaks.com/topic/181934-looping-a-form-based-on-how-many-inputs-user-makes/#findComment-959763 Share on other sites More sharing options...
mikesta707 Posted November 18, 2009 Share Posted November 18, 2009 Do you know anything about the HTML DOM that Javascript has? Check out this tutorial. Pay special attention to getElementById(), and the .innerHTML attribute of tags. If you need extra help your best bet would be to post in the javascript section Quote Link to comment https://forums.phpfreaks.com/topic/181934-looping-a-form-based-on-how-many-inputs-user-makes/#findComment-959767 Share on other sites More sharing options...
ngreenwood6 Posted November 18, 2009 Share Posted November 18, 2009 You might also want to look into jquery. It is just a wrapper for javascript but it makes working with the page alot easier and alot less code involved. Quote Link to comment https://forums.phpfreaks.com/topic/181934-looping-a-form-based-on-how-many-inputs-user-makes/#findComment-959769 Share on other sites More sharing options...
cbr_600rr Posted November 18, 2009 Author Share Posted November 18, 2009 thanks guys, ill check into those things Quote Link to comment https://forums.phpfreaks.com/topic/181934-looping-a-form-based-on-how-many-inputs-user-makes/#findComment-959786 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.