Help with dynamic input fields - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Help with dynamic input fields (/showthread.php?tid=31223) |
Help with dynamic input fields - El Forum - 06-10-2010 [eluser]Markie577[/eluser] Hi People, I have a quistion, I have a form with a submit to a function in my controller This form is dynamically updated trough jquery. So everytime I click a button, It will add another input field. I have a var i in my jquery which add's a number after my input field. Example <input id=car'+i+' value=dynamic> id car1 value=Volkswagen *click button* id car2 value=Audi *click button* id car3 value=Mercedes *click button* Submit Now I want my controller function getPost to get the POST data from the inputs. so $car = this->input->post('car???') But I have no idea how to get these different car[i]! perhaps a for(i=0; i????; i++ { $car[i] = this->input->post('car???') } Help with dynamic input fields - El Forum - 06-10-2010 [eluser]mddd[/eluser] Code: // get all keys in $_POST and check if they match 'car'. Help with dynamic input fields - El Forum - 06-10-2010 [eluser]Markie577[/eluser] [quote author="mddd" date="1276196316"] Code: // get all keys in $_POST and check if they match 'car'. Ehmm perhaps you could help me understand this code what is $key and $v? then you have /car\(d+)? Sorry I'm not getting the code! Help with dynamic input fields - El Forum - 06-10-2010 [eluser]yohanip[/eluser] some notes : 1. Instead of using id, try to use : Code: <input type="text" name="cars[]" value="MerciGracias"/> Code: $cars = $this->input->post('cars'); Help with dynamic input fields - El Forum - 06-10-2010 [eluser]mddd[/eluser] Code: foreach($_POST as $key=>$v) Code: if (preg_match('/^car\(d+)$/', $key, $match)) That's about it. Btw I made a typo, it should be $this->input->post($key), sorry. I corrected it in my post above. I understand that using preg_match can be confusing, but it is a fast way of comparing strings. You could do it differently I guess. Help with dynamic input fields - El Forum - 06-10-2010 [eluser]ben_rivero[/eluser] the code of yohanip is definitely the best way to do that... Now i want extend the question??? What if i need to evaluate this extra fields (using the functions of codeigniter) And if there's a problem with the data of one or more of this fields.. repopulate using set_value... is this posible??? or i have to do that on my own?? |