How to make a form with the number of variable fields and insert this data into the database?

Asked

Viewed 582 times

3

Hello.

I want to create a CRUD that does word management and its synonyms. A word has several synonyms (1:N).

I would like a solution to dynamically include the inputs, according to the demand of each user. For example, one word to be registered has 3 synonyms, while another word has 10 synonyms. There in the first example, 3 fields would be created, and in the second, 10 fields.

How to make this form and how to insert this data into the database? I am using PHP + Mysql.

  • What have you tried?

  • I tried to put a form with a field for the user to enter the number of synonyms, then send via Post and show the registration form with the right number of fields. But I did not find this solution good, until pq I will have to make another form that this situation will repeat more than once.

  • 1

    you have doubt on how to add one more input text ? and how to receive in php?

  • I would like a solution to dynamically include inputs, according to the demand of each user. For example, one word to be registered has 3 synonyms, while another word has 10 synonyms. There in the first example, 3 fields would be created, and in the second, 10 fields.

  • 1

    Aaaaaah now yes, I think everyone understood what you. To 'duplicate' the inputs it is necessary to use javascript or jquery, an important detail the name of this input had the brackets ex: '<input type="text" name="sinonimo[]" /> so php will understand that it’s an array. You can do a simple test, create two text with the same name and then do the one foreach in php, ex foreach($_POST['sinonimo'] as $item){ echo $Item.'<br>';}. Edit your question and put part of your comment on it, it will make it easy for someone to answer.

  • I’ll edit it, thank you.

  • 1
  • The question is cool now, it can be better if you put part of the code you did, if it didn’t start try :). Go in stages, first mount the form and only, then try to make the code that assembles the dynamic inputs work, receive the values in php and finally write to the database.

  • 1

    I managed to do. Thank you very much!

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.