ayampanggang Posted July 19, 2007 Share Posted July 19, 2007 let's say i have a html form with textboxes that can be dynamically added with javascript. i'd like to save each of the textbox's value into a new different row in the database. so i'll have this in my html form: <form action='/add' method='POST'> <a href='javascript:;' onclick='addTextBox()'>Add New Field</a> <!-- new textfield/textbox will be added dynamically everytime the user clicks</a> </form> so basically i should do something like this in the serverside: for ($i = 0; i < totalTextBoxes; ++i) { // iterate every textbox in the html form mysql_query("insert into `books` value (textBoxName[i]); // for every textbox save it into a new row in the table } the problem is, since i created the textbox dynamically using javascript, how can i tell php how many textbox that i actually have there? thank you very much, any help will be appreciated. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 19, 2007 Share Posted July 19, 2007 From your code, i gather that the text boxes are named as an array? So you would have textBoxName[0], textBoxName[1], textBoxName[2] ect as field names? If so, couldn't use use the count() function on the textBoxName array? Other than that, you could always store the number of text boxes added in a hidden field. Quote Link to comment Share on other sites More sharing options...
ayampanggang Posted July 19, 2007 Author Share Posted July 19, 2007 i just noticed that we can actually create an array by using the square brackets in the input name. i think using it and count() function will be a good idea thanks for your help, i'll post later if i have more problems. thank you! Quote Link to comment Share on other sites More sharing options...
dbo Posted July 19, 2007 Share Posted July 19, 2007 When I did something similar I kept a hidden field with a "counter" variable and incremented it each time a new row was added and then prefixed my input with the counter variable. In my instance I wasn't letting them remove rows... but if they did you could use the counter as your upper bound of a loop and use isset to determine if any rows were missing. 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.