Re: PHP strings -- newbie

From: Ashmodai (ashmodai_at_mushroom-cloud.com)
Date: 01/30/05


Date: Sun, 30 Jan 2005 04:06:19 +0100

Josh scribbled something along the lines of:
> Ok... here is an excerpt of the code to show what I am trying to do. I want
> the page NOT to print out the results further down the page if there are any
> fields left blank. If that is not possible I will make it into two pages.
>
> // Check $lastName and strip any slashes:
> if (strlen($lastName) > 0) {
> $name = stripslashes($lastName);
> } else { // If no name was entered...
> $name = NULL;
> echo '<p><b>You forgot to enter your last name.</b></p>';
> } // THIS IS WHERE I WANT THE PAGE TO STOP LOADING
> // Check $email1 and strip any slashes
> if (strlen($email1) > 0) {
> $name = stripslashes($name);
> } else { // If no name was entered...
> $name = NULL;
> echo '<p><b>You forgot to enter your email address.</b></p>';
> }
> // Check to see that email fields match
> if ($email1 != $email2) {
> echo '<p><b>Your email fields must match.</b></p>';
> }
> // Check $creditCardNumber and strip any slashes:
> if (strlen($cardnumber) > 0) {
> $name = stripslashes($cardnumber);
> } else { // If no name was entered...
> $name = NULL;
> echo '<p><b>You forgot to enter your credit card number.</b></p>';
> }
>
>

exit() kills the script execution, but I kinda think that's not the best
solution here.

I think you should make an array named "errors" ($errors = array()) and
add a key ($errors[] = "...";) for every error that occurs. Then, at the
end of the validation, you should check if the array is empty and do
your magic (i.e. whatever you intend to do with the validated
information) if the array is NOT empty (if(!empty($errors)){...}) or
display the errors (else {foreach($errors as $error) {...}}).

It's quite simple, but it works.

Another approach is just setting a flag ($errors_found = TRUE) whenever
you encounter an error and then check whether the flag has been set
(isset(!$errors_found)) or check its value ($errors_found != TRUE) in
order to determine whether there were any errors, but that reeks.

--
Ashmo


Relevant Pages

  • Re: [PHP] your method for validating forms
    ... I use a validation class that's kind of like this. ... be validated, a flag to allow empty values, and a flag to set a required ... the class makes a $css array entry with a key ...
    (php.general)
  • Re: DatagridView Validation assistance needed
    ... You can probably avoid your loop problem by setting a flag and only do ... the validation when you are not trying to explicitly move to the empty ... bool inValidateMove = false; ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: random array elements and speed
    ... variable number of random elements with each pass of a loop that runs ... I cached the unmodified array, and then with each pass enumerate it as ... It's a flag method. ... Rate splice swap_pop flag ...
    (comp.lang.perl.misc)
  • Re: Post input processing and security
    ... |> as for escapeData, you can cause yourself headaches by assuming all data ... |> you provide no means to override that functionality. ... your namelist is an array ... |> can apply the validation and generate errors for the end-user. ...
    (alt.php)
  • Re: Macros in php
    ... From the PHP manual for empty: ... "The following things are considered to be empty: ... array() ... practice in PHP. ...
    (comp.lang.php)