Ok,

Let's concentrate on acceptable syntaxes for your html programmers to specify the 
required fields
first. Here are some options grouped by technique and roughly put into order of 
increasing
difficulty for an html author:

 
  HTML
  ----
1. <INPUT TYPE="HIDDEN" NAME="required" VALUE="name address phone">

  PHP FUNCTION
  ------------
2. required(name,phone,address);
3. required($name,$phone,$address);

  PHP ASSIGNMENT
  --------------
4. $required = 'name address phone';
5. $required = 'name,address,phone';
6. $required = array($name,$address,$phone);

  COMPLETE INLINE PHP CODE
  ------------------------
7. Rasmums solution (slightly modified): 
if (!(1==1 
     && isset($name)
      && isset($address)
      && isset($phone)
)){print 'You left one empty.';}
       

It's really up to you to say which of these is most acceptable to your html guys.  The 
order also
roughly corresponds to decreasing amounts of php code and support required.

Here's the supporting code for each:

1. <INPUT TYPE="HIDDEN" NAME="required" VALUE="name address phone">
-------------------------------------------------------------------
foreach(explode(' ',$required)) as $field)
{
   if (${$field) == '')
   {
      print 'You left one empty.';
      break;
   }
}

2. required(name,phone,address);
--------------------------------

function required()
{
   foreach(func_get_args() as $field)
   {
      global ${$field};
      if (${$field} == '')
      {
         print 'You left one empty.';
         return;
      }
   }
} 


3. required($name,$phone,$address);
-----------------------------------

function required()
{
   foreach(func_get_args() as $field)
   {
      if ($field == '')
      {
         print 'You left one empty.';
         return;
      }
   }
} 


4. $required = 'name address phone';
------------------------------------

foreach(explode(' ',$required) as $field)
{
   if (${$field} == '')
   {
      print 'You left one empty.';
      break;
   }
}      

5. $required = 'name,address,phone';
------------------------------------

foreach(explode(',',$required) as $field)
{
   if (${$field} == '')
   {
      print 'You left one empty.';
      break;
   }
} 

   
6. $required = array($name $address $phone);
--------------------------------------------

foreach(explode(' ',$required) as $field)
{
   if (${$field} == '')
   {
      print 'You left one empty.';
      return;
   }
} 
   

7. Rasmus Solution 
------------------
Already complete!


The closest to your original is 4., and it is pretty close.  However, if your html 
guys are really
so allergic to code I would have thought that 1. would suit them best. The great 
benefit of 7., is
that your html guys would learn something useful that they could apply elsewhere 
rather than a
special "rule" on where to put required fields when they're working with you on a 
particular kind of
job. 

For the record I typically have a completely different approach to the whole problem 
i.e.

A. I would have field-specific (onChange) and form-wide (onSubmit) Javascript 
validation on the form
page itself as a courtesy to the browser users and to save them unnecessary page loads.

B. In the php, the checks would be repeated explicitly for users without Javascript or 
non-browser
users and would include an unique identifier so that the form could be intelligently 
driven remotely
by another application e.g.
   if ($name == '')
   {
       $errormessage .= "error:myform:0100 name is a required field.<br>";
   }
 
   if ($address == '')
....

   if ($errormessage != '')
   {
      print 'Please correct the following errors -<br>'.$errormessage;
etc.

C. The data would almost certainly have ended up in a database even if it is being 
mailed on or
whatever. So the question of non-programming html designers adding required fields 
would not arise. 
I can't think of much data that I might trouble a user to enter that isn't worth 
explicitly storing.

But then I guess we work on different kinds of applications.  It sounds like you can't 
afford more
than a couple of minutes per form while I have the luxury of half an hour or so.

Good luck,

George

Brandon Lamb wrote:
> 
> The point of keeping it easier is what if i want to give my script to a
> friend, do you REALLY want to explain to a non-programmer how to add another
> if statement or condition when they could simply add the field to the array?
> 
> And actually you only have to change the name in 2 places.
> 1. you define the variable as an input from a form
> 2. in the required fields array
> 
> ----- Original Message -----
> From: "George Whiffen" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Wednesday, November 21, 2001 7:28 AM
> Subject: Re: [PHP] How do I convert from perl to php? - Reality Check &
> Taxation
> 
> > So I would have to write a seperate if condition for each form input field
> i wanted to require? that doesn't make for a very dynamic script...
> >
> > > if(!(isset($name) && isset($address) && isset($phone)) {
> > >     echo "You left one empty.";
> > > }
> 
> Reality Check:  We write code to solve real world problems!
> 
> The parts of our code which are essential to the describe the real world
> problem we want solved are
> essential.  All the rest of our code is an unfortunate "tax" on the rest of
> the world.  The code can
> be as complex, dynamic, interesting or clever as it likes, it's still tax!
> "Very dynamic script"s
> have to be JUSTIFIED, they are not, repeat not, intrinsically good!
> 
> In this case, the essential elements are the names of the fields required
> and the message to be sent
> if they are not present i.e. the following 38 characters
> 
> "name address phone You left one empty."
> 
> Rasmus code consists of 92 characters i.e. 44 extra characters or around
> 110% tax.  Does that sound
> a lot?  Your original perl had 192 characters i.e. over 400% tax.
> 
> What about maintainability/reusability?  Lets look at the tax element of
> some likely changes:
> 
> 1. Change in the name of one of the required fields e.g. name should now be
> lastname
> Rasmus : 0% TAX:  (You change "name" to "lastname" once)
> Perl : 200% TAX:  (You change "name" to "lastname" in 3 places)
> 
> 2. Remove one of the fields from the required list
> Rasmus : 12 characters TAX (You have to remove "&& isset($) " as well as the
> field name itself)
> Perl : 13 characters + 200% TAX (You must remove "$ = param($);\n" and the
> field name 3 times)
> 
> 3. Add a new field
> As per 2. above.
> 
> 4. Modify the conditions for the error message e.g. change to name and
> either address or phone
> required
> Rasmus : 4 characters TAX (change && to "or" and add two brackets) i.e.
>     if(!(isset($name) && (isset($address) or isset($phone)) {
> Perl : Rewrite required....unknown cost!
> 
> Well, I hope that resolves the question of which is the more world-friendly
> code (i.e. more "tax"
> efficient).
> 
> Personally, and all views on simplicity, elegance and beauty of code are
> subjective, I also find
> Rasums php version much simpler and easier to understand.  It involves far
> fewer commands and is
> therefore much more accessible to the novice programmer.  It has much less
> extraneous structure and
> is clearly focussed on the task in hand.  It can very easily be extended and
> modified to provide
> richer functionality.  What more do we want? (Well personally, I'd rather he
> used "and" instead of
> "&&" and "not" instead of "!" and put the separate conditions on separate
> lines and generally had
> more white space ;).
> 
> George
> 
> P.S.  Is this a characteristic example of the difference beteeen Perl and
> PHP or an extreme
> example?  Is Perl really so "geeky" in style and application?  Or am I just
> too stupid, stubborn,
> ignorant to see that Perl is better than php?
> 
> [EMAIL PROTECTED] wrote:
> >
> > So I would have to write a seperate if condition for each form input field
> i wanted to require? that doesn't make for a very dynamic script...
> >
> > > if(!(isset($name) && isset($address) && isset($phone)) {
> > >     echo "You left one empty.";
> > > }
> > >
> > > On Tue, 20 Nov 2001 [EMAIL PROTECTED] wrote:
> > >
> > > > I am a perl user trying to convert to php
> > > >
> > > > how would i turn this perl into php?
> > > >
> > > > use CGI;
> > > >
> > > > $name = param(name);
> > > > $address = param(address);
> > > > $phone = param(phone);
> > > >
> > > > @required = qw( name address phone );
> > > >
> > > > foreach $key($required)
> > > > {
> > > >  if (!$$key) { &out("You left one empty."); }

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to