Re: PHP strings -- newbie
From: Ashmodai (ashmodai_at_mushroom-cloud.com)
Date: 01/30/05
- Next message: RootShell: "Newsgroups Class PHP (posting)"
- Previous message: Ashmodai: "Re: autocomplete"
- In reply to: Josh: "Re: PHP strings -- newbie"
- Next in thread: DAP: "Re: PHP strings -- newbie"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: RootShell: "Newsgroups Class PHP (posting)"
- Previous message: Ashmodai: "Re: autocomplete"
- In reply to: Josh: "Re: PHP strings -- newbie"
- Next in thread: DAP: "Re: PHP strings -- newbie"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|