FreePgs.com Forum

FreePgs Related => Support Requests => Topic started by: sixthcrusifix on January 20, 2006, 03:25:00 AM

Title: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: sixthcrusifix on January 20, 2006, 03:25:00 AM
For some reason even if you input NOTHING into the form fields as long as you push submit the script runs as if all the fields were set to something.


<?php


if(!isset($_POST['name'],$_POST['games'],$_POST['reason'])){
//nothing, everything's just starting. 
echo("TEST");
}else if(isset(
$_POST['name'],$_POST['games'],$_POST['reason'])){ 

$name_ $_POST['name']; $games_  $_POST['games']; $reason_ =  $_POST['reason'];

echo(
"$name_ $games_ $reason_");

$msg "Account Name: $name_ \n";
$msg .= "Games Made: $games_ \n";
$msg .= "Reason: $reason_ \n";
$to "join_pro@gdmail.com";
$sub "Account Request";

mail($to,$sub,$msg) ;
echo(
"<script> alert(\"SUCCESS! Account request mailed!\")</script>");


} else{echo(
"You Left Out A Field!");}


?>

Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: admin on January 20, 2006, 03:34:20 AM
This is because you are only testing if the variables exist.  Even if the input is blank, the variable itself still exists.

Thank you,
FreePgs.com Admin
Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: brainiac744 on January 20, 2006, 03:40:27 AM
Try something like this:

<?php

if ($_POST['name'] != "" && $_POST['games'] != "" && $_POST['reason'] != "") {

$name_ $_POST['name'];
$games_  $_POST['games'];
$reason_ =  $_POST['reason'];

echo(
"$name_ $games_ $reason_");

$msg "Account Name: $name_ \n";
$msg .= "Games Made: $games_ \n";
$msg .= "Reason: $reason_ \n";
$to "join_pro@gdmail.com";
$sub "Account Request";

mail($to,$sub,$msg) ;
echo(
"<script> alert(\"SUCCESS! Account request mailed!\")</script>");

}

else {
echo(
"You Left Out A Field!");
}


?>
Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: webzone (archived) on January 20, 2006, 03:55:53 AM
Quote$_POST['name'] != ""

Why not : !empty($_POST['name'])
Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: brainiac744 on January 20, 2006, 04:00:20 AM
Sure, that works too :) (Just for my information, aren't those both basically 2 different ways of achieving the same effect?)
Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: webzone (archived) on January 20, 2006, 04:19:18 AM
Quotearen't those both basically 2 different ways of achieving the same effect?

I'm not sure, but the PHP documentation makes me think that empty (http://php.net/function.empty) is a more complete check.

QuoteThe following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

In any case, empty() is more meaningful when reviewing the code, IMO. This is why I tend to use it.
Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: sixthcrusifix on January 20, 2006, 04:22:48 AM
Quote from: webzone on January 20, 2006, 04:19:18 AM
Quotearen't those both basically 2 different ways of achieving the same effect?

I'm not sure, but the PHP documentation makes me think that empty (http://php.net/function.empty) is a more complete check.

QuoteThe following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

In any case, empty() is more meaningful when reviewing the code, IMO. This is why I tend to use it.

oh thanks. isset always used to work for me I dunno why this is differnt. I think i'll start using empty. :D
Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: webzone (archived) on January 20, 2006, 08:01:47 PM
isset checks if the variable was set, even if it was set of an empty value. empty will check if the variable contains something non-empty.

for instance, this code should display "TRUE FALSE" (ie. isset returned true and !empty returned false).
<?php
$some_var 
"";
if(isset(
$some_var)){ echo "TRUE "; }else{ echo "FALSE ";}
if(!empty(
$some_var)){ echo "TRUE "; }else{ echo "FALSE ";}
?>
Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: sixthcrusifix on January 21, 2006, 07:44:40 PM
Quote from: webzone on January 20, 2006, 08:01:47 PM
isset checks if the variable was set, even if it was set of an empty value. empty will check if the variable contains something non-empty.

for instance, this code should display "TRUE FALSE" (ie. isset returned true and !empty returned false).
<?php
$some_var 
"";
if(isset(
$some_var)){ echo "TRUE "; }else{ echo "FALSE ";}
if(!empty(
$some_var)){ echo "TRUE "; }else{ echo "FALSE ";}
?>




Will empty return an error if the variable wasn't set at all? This gets a bit wacky. I don't wanna have to use both . . .
Title: Re: WHY THE HELL IS THIS SCRIP RUNNING WHEN THE IF REQS AREN'T MET!?!??!?
Post by: webzone (archived) on January 22, 2006, 01:42:31 AM
empty() checks if the variable is empty or if it wasn't set at all. so, it will work even if the variable was never created.

to my knowledge, only very few functions in PHP generate an error if they receive an empty variable.