Validation Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Validation Question

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

mmm... no man, they have different names, not like field[1], field[2], ...

but anyway thanks for the suggestion. :D
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

sugestion

Post 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"]].
Post Reply