Jump to content

Recommended Posts

I want to create elements dynamically ..

for example:

when user select 3 in a list box I want 3 txtboxes (or other elements) to appear in the form ..

if he press 2 (and 3 already selected then the 3 boxes disappear) and  2 textboxes shows.. etc..

 

and how should I name the dynamically created elements in a way that I know how to retreive them in the $_POST array and place them correctly in their place in the database. ?

Link to comment
https://forums.phpfreaks.com/topic/139085-create-elements-dynamically/
Share on other sites

I typically do the input name + the number, for both the name/id

 

So, you have a dropdown for X amount of "names"

<input name='name1' id='name1'>

<input name='name2' id='name2'>

....

 

Then

<?php
if(isset($_POST['name1']) && !empty($_POST['name1'])) {
// stuff
}
// OR
// Change $i = to your max num of inputs you have. 
for($i=3; $i>0; $i--) {
   $name = 'name'.$i;
   // Set the name correctly, and then see if that variable is set.
   if(isset($_POST[$name]) && !empty($_POST[$name])) {
       // do stuff
   }
}

 

Just one way of many to do that.

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.