You are here: Re: [PHP] Dynamic Array Index using Variable « PHP « IT news, forums, messages
Re: [PHP] Dynamic Array Index using Variable

Posted by Al on 01/10/06 04:29

Chris Lott wrote:
> What I have is a form with 45 data fields... if any of a selected 35
> of those fields is empty, I want to have the user go back. So I can't
> loop over the entire _POST array. So I thought I would put the names
> of the fields I *do* want to check into an array and check.
>
> So this won't work:
>
>>foreach($array as $key=>$value){
>> if(empty($value)){
>> $err_msg= "$key is empty";
>> break;
>
>
> Because the array is the names of the fields, and I want to check each
> variable in _POST that matches. So instead of writing
>
> if (empty($_POST['forma'])
> if (empty($_POST['formb'])
> etc
>
> I would do something like
>
> $fieldstocheck = array('forma', 'formb');
>
> foreach ($fields as $thisfield) {
> if (empty($_POST[$thisfield])) {
> set the error flag;
> }
>
> But that doesn't seem to work.
>
> Eventually I will use the array to associate error text:
> $fieldstocheck = array('forma' => 'You must enter field A', 'formb' =>
> ''Field B is your age in years and is required');
>
> etc
>
> c

Simple...

Make an array of the field names to test, $check_fields= array('field1', ....). Then

foreach($check_fields as $field){

if(empty($_POST[$field])) $err_array[]= $field;
}

$err_array is a list of the empty fields.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация