Link to home
Create AccountLog in
Avatar of wilson1000
wilson1000

asked on

Checking the values of a repeated input field

Hello,

I need to check the values of an input field, something similar to

  if ((isset($_POST['ins_cont_fina'])) && ($_POST['ins_cont_fina']!=""))

The problem is that the 'ins_cont_fina' field is part of a repeated region and is formatted like this

  <input type="text" name="ins_cont_fina_<?php echo $repCounter ?>" />"

I'm not great with array's at the moment and I think i need to check the values within an array to determine if any of the values state 'First Name', if they do, then the global var "$_POST['ins_cont_fina_?']" (which ever counter number it is) must be cleared.

Could anyone explain how this could be made possible please?
Avatar of hernst42
hernst42
Flag of Germany image

You could either use different name for the form like:
  <input type="text" name="ins_cont_fina[<?php echo $repCounter ?>]" />"
Then  $_POST['ins_cont_fina'] wourld be an array where you could loop over:

foreach( $_POST['ins_cont_fina'] as $repCounter => $value) {
   if ($value != '') {
     // do something
   }
}

Other option is to loop over al variables and filter the ones out like:
forach($_POST as $k => $v) {
   if (strpos($k, 'ins_cont_fina_') === 0 && $v != '') {
     // do something
   }
}
Avatar of wilson1000
wilson1000

ASKER

Thanks,

I'm really sorry, my knowledge of manipulating array's is almost non-existent - i'm not sure I understand.

What I need to do is clear the value of ins_cont_fina if it equals the string : `First Name` - this should happen before the data is inserted into the data base.

You see... at the moment, if ins_cont_fina has a value then a record will be created in the database. The only problem with this is, if the value is First Name, the information hasn't been filled in and a row must not be created. PHP then carries on looping to insert any other records that have a value in this field.

Which one of your options would be the better choice in this circumstance?

Thank you for your patience.
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hi hernst,

I think the code above is recognizing a value of First Name and then clearing every $_POST value there is.

The only value that should be cleared is the ins_cont_fina field.

If I'm incorrect, would you be kind enough to explain how it works??

Thank you
Thank you