Jump to content

how to add text fields to form dynamicly and send to database


alapimba

Recommended Posts

Hi.

 

I need to fo a form and the content be sent to a database but i'm stuck...

I knwo hot to do forms and also how to send data to mysql using php.

 

But now i need something a bit more complex and i'm stuck.

 

I need to make a form lets say with "parent name, age, child name, child age"

Some people has 1 child some people has 2 3 4...and i need to have it separated and not just write like "joe, bob, jack" in the same line.

How can i do this?

 

And how should the database be structured? i can't put "id, parent, age, c_name, c_age" because some ppl will need to have more than 1 child in the database.

 

Don't know what to do :(

 

Link to comment
Share on other sites

I'd probably first create two tables. Lets call one "parent" and the other "child". The parent name and ID can be stored in the parent table while the children will be stored in the child table with a parent ID and of course their own unique id. That's a relational database for you...

 

The javascript seems pretty straight forward. Not sure how you are doing it, but lets say you have 10 inputs for children name and age, just to make it simple, with the name input being "child_name" and age being "child_age". Assuming the user enters the data correctly, you can use PHP to loop through it because those inputs will be an array. For example, if you post the data, you can loop through $_POST['child_name']. I can't say for sure if several elements will be empty, but I'm pretty sure all 10 would be posted.

 

So, a better way is to have them enter how many children they have and dynamically create those inputs, or just have an "Add Child" button.

 

Hopefully this gets you on track.

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.