Page 1 of 1

Validation Question

Posted: Fri Oct 25, 2002 12:05 am
by rax369
Hi people :D , my question now is how to know if at least one of my forms elements changed its default value or which of those where changed by the user??

This is my case... I have a form with around 8 - 10 drop down menus, and I want to know once the information has been submited if at least one of the drop down menus changed its default value ('Choose one...')

Their defaul value is 'Choose one...' because that's what I putted inside the HTML sentence <option selected> <? echo "Choose one..."; ?> </option> mmm... I did this to show some direction to the user, and not leave all those drop down menus with the last value asigned which are coming from querys, I didnt like how they looked, showing some value not assigned by nobody.

I need to know this 'cause know I have to perform a validation where the condition will validate all those variables, and I dont want to do something like this:

if ( $_POST[variable1] != "Choose one..." OR $_POST[variable2] != "Choose one..." :evil: OR $_POST[variable3] != "Choose one..." :evil: OR .... AND SO ON...)
{
action 1;
action 2;
.
.
.
}

Got it :?: , this way would be a very... very long validate sentence.... so if there is some php function to perform this task I wanna achieve, shoot me that function please, or light my mind w/some solution.

Thx again guys. :oops:

Posted: Fri Oct 25, 2002 2:38 am
by volka
if you use names like fields[1] in your form
i.e.

Code: Select all

&lt;form...&gt;
   &lt;SELECT name="field&#1111;1]"&gt;...&lt;/SELECT&gt;
   &lt;SELECT name="field&#1111;2]"&gt;...&lt;/SELECT&gt;
   &lt;SELECT name="field&#1111;3]"&gt;...&lt;/SELECT&gt;
&lt;/form&gt;
you may use array_reduce
i.e.

Code: Select all

function checkFieldsChanged($result, $current)
{
   if ($current != ''Choose one...')
      $result++;
   return $result;
}
...
if (array_reduce($_POST&#1111;'fields'], 'checkFieldsChanged') != 0)
  // something has changed

Posted: Fri Oct 25, 2002 10:03 am
by rax369
mmm... no man, they have different names, not like field[1], field[2], ...

but anyway thanks for the suggestion. :D

sugestion

Posted: Fri Oct 25, 2002 1:02 pm
by AVATAr
you can use volka suggestion but you can use:

Code: Select all

&lt;?php
&lt;form...&gt; 
   &lt;SELECT name="field&#1111;name]"&gt;...&lt;/SELECT&gt; 
   &lt;SELECT name="field&#1111;address]"&gt;...&lt;/SELECT&gt; 
   &lt;SELECT name="field&#1111;tel]"&gt;...&lt;/SELECT&gt; 
&lt;/form&gt;
?&gt;
and then you use foreach to go through the array $field (as i show you in other post), or directly with $_POST[field["address"]].