Jump to content

create elements dynamically


hassank1

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.

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.