Forum Moderators: coopster

Message Too Old, No Replies

Can anyone tell me what I am doing wrong here?

Does return() act as an exit;

         

Matthew1980

9:39 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there people of the forum,

I am currently trying to get to grips with class scripts, and struggling. I would appreciate some advice/suggestions on this, which is obviously wrong. The objective is to check for empty fields, and if they are populated, carry onto the valid input checks. Hopefully I have the idea right...

function EmptyOrFull(){

foreach($_POST AS $key=>$value){

if($key == "username" && $value == ""){
return(1);
}elseif($key == "password" && $value == ""){
return(2);
}else{
return(0);
}
}
}

Again, any advice would be great.

Cheers,
MRb

Readie

10:04 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, try this:
function EmptyOrFull() {
foreach($_POST as $key => $value) {
if($key === "username" && empty($value)) {
$ret = 1;
break;
} elseif($key === "password" && empty($value)) {
$ret = 2;
break;
} else {
$ret = 0;
}
}
return $ret;
}
I think what was happening there was the function was continuing the foreach() after you had sent a return, so I'm using break; to stop the loop.

rocknbil

2:06 am on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right . . . question is, what is the context of your function? I use them like this. It's a simplified example, but you can see the idea . . .


$requireds = Array (
'username' => '\w+',
'password' => '[a-z\d]+',
'first_name' => '\w+',
'quantity' => '\d+', // just throwing a numeric in there
'last_name' => '\w+'
);
//
$errors = check_data($requireds);
if ($errors != '') {
echo "<p>Errors in your form:</p><ul>" . $errors . "</ul>";
// output form here
}
else {
// process the form
}
//
function check_data($requireds){
$dataErrors = ''; // or you could use null
foreach($_POST AS $key=>$value){
if (! preg_match("/^$requireds[$key]$/i",$value)) {
$dataErrors .= "<li>The " . $key . " field is blank or contains invalid input.</li>";
}
return $dataErrors;
}



Just one of the cases where regexps are an easy solution over PHP functions . . . but if you wanted to use those instead of regexps, you can figure out how to get the function calls (is_numeric, etc.) in the array instead of patterns.

(Go forth and debug, I have to have typos in there somewhere :-) )

Matthew1980

7:07 am on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie & Rocknbil,

Readie: Tried your sollution and only returns the same as mine, But I figured as it would as you can't return two distinct values at the same time, unless you return an array! But since doing this (and having some well needed sleep!) I realised that I wasn't accounting for BOTH fields being null! Back to the drawing board...

Rocknbil: The context is a login class. Sounds simple, and should be, but I don't know whether I am 'over engineering' things. Basically I want to be able (from submit being actioned) to check & highlight individual fields if null; perform sanitising & pattern checks if fields hold data; if data correct/allowed, check to see if username taken & password wrong or right, and finally redirect to a defined location, depending on the results from the previous functions...

I am starting to think as I am running too fast though, I can do all of the above no issue, so long as it is sequential and not OOP.

Thanks for the code as you provided too, I shall check that when I get chance!

Cheers,
MRb