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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.