CodeIgniter Forums
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'.
foreach($_POST as $key=>$v)
{
  if (preg_match('/^car\(d+)$/', $key, $match))
  {
    $id = $match[1];
    $value = $this->input->post($key); // we get it through the Input class, so we are able to use its security features etc.
    // now do something with the id and the value.
  }
}



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'.
foreach($_POST as $key=>$v)
{
  if (preg_match('/^car\(d+)$/', $key, $match))
  {
    $id = $match[1];
    $value = $this->input->post($match[$key]); // we get it through the Input class, so we are able to use its security features etc.
    // now do something with the id and the value.
  }
}
[/quote]

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"/>
<input type="text" name="cars[]" value="BMW"/>
<input type="text" name="cars[]" value="Motorola"/>
2. on your controller simply use this :
Code:
$cars = $this->input->post('cars');
//nuw we try a var_dump to see what the variable really look like
var_dump($cars)



Help with dynamic input fields - El Forum - 06-10-2010

[eluser]mddd[/eluser]
Code:
foreach($_POST as $key=>$v)
This simply says we're looking at all the values in $_POST and we want to look at the key (the name of the POST variable) and its value. We call them $key and $v (I use a short name like $v for a variable that I'm not really going to do much with -- because we are really only looking at the names here).

Code:
if (preg_match('/^car\(d+)$/', $key, $match))
Check out preg_match in the php manual. It's not so complicated but you have to know about regular expressions that are used for comparing stuff. We are matching the name (that is in $key) to the pattern '/^car(\d+)$/' which means: start with the letters 'car' and then any number. The number after 'car' is saved to $match[1].

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??