Jump to content

Saving data into database from a dynamically-added textbox


ayampanggang

Recommended Posts

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. :)

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.

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! :)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.