...

View Full Version : Where's The Error?



craighowarth
11-04-2006, 03:03 PM
$user = $_POST['user'];
$score1 = $_POST['score1'];
$score2 = $_POST['score2'];

if (empty($user) || empty($score1) || empty($score2))
{
echo '<div align="center" <span class="style1"><strong>ERROR:</strong> You left one or more fields empty, please complete all fields and try again.</span></div>';
include 'prediction+league.php';
exit();
}
else

$message = "Craig...
$score1
$score2";

mail("craig.howarth@toon-army.co.uk", "Predictions",
$message, "From: $user");
echo '<div align="center" <span class="style1">Your predictions have been sent successfully.</span></div>';

The code doesn't work, no matter what if all the fields have text in it still brings up the error message, but still sends the information to the email address.

Thanks in advance.

Craig

CFMaBiSmAd
11-04-2006, 04:00 PM
About the only way it would display the "ERROR: You left one or more fields ..." message and send an email is if the include file contains code that sends an email.

alexpayne
11-04-2006, 04:03 PM
to start the syntax is incorrect :)
try the testing bits below.. and check that 'empty' is not displayed.


$user = $_POST['user'];
$score1 = $_POST['score1'];
$score2 = $_POST['score2'];

//testing
echo $user;
if(empty($user)) echo "empty";
echo $score1;
if(empty($score1)) echo "empty";
echo $score2;
if(empty($score2)) echo "empty";
//

if (empty($user) || empty($score1) || empty($score2)){
echo '<div align="center" <span class="style1"><strong>ERROR:</strong> You left one or more fields empty, please complete all fields and try again.</span></div>';
include 'prediction+league.php';
exit();
}else{
$message = "Craig...
$score1
$score2";

mail("craig.howarth@toon-army.co.uk", "Predictions",
$message, "From: $user");
echo '<div align="center" <span class="style1">Your predictions have been sent successfully.</span></div>';
}

iota
11-04-2006, 04:31 PM
why not isset()?

Fumigator
11-04-2006, 05:25 PM
The trick with empty() is it will return TRUE if the value you send is 0 or "0". This is just a guess but based on your variable names ($score1 and $score2) it's likely one of those values is "0" and therefore doesn't pass the empty() test the way you'd expect.

The reason the mail is being sent is because you didn't wrap your code block up with brackets {} in the "else" logic so the only statement that gets run with the "else" statement is the $message assignment. Everything below that will be run every time.

craighowarth
11-04-2006, 11:15 PM
The trick with empty() is it will return TRUE if the value you send is 0 or "0". This is just a guess but based on your variable names ($score1 and $score2) it's likely one of those values is "0" and therefore doesn't pass the empty() test the way you'd expect.

The reason the mail is being sent is because you didn't wrap your code block up with brackets {} in the "else" logic so the only statement that gets run with the "else" statement is the $message assignment. Everything below that will be run every time.

So how will i get around that?

What shall i put instead of empty()?

rafiki
11-05-2006, 01:24 PM
would this work


$str = '$score1';
if (strlen($str) < 1) {
// your code here for score existing
else
{
echo" You missed out a score";
}

Fumigator
11-05-2006, 06:12 PM
Yep you could use strlen($str) > 1, or if($str > '') is probably another safe bet.

craighowarth
11-05-2006, 07:27 PM
Sorry i'm abit new (and thick) to PHP, could you add the code needed into mine.

Cheers:thumbsup:

rafiki
11-06-2006, 01:39 PM
Hi craig,
heres a script i wrote up for you, should work and you may need 2 make minor changes.




<?php

$user = $_POST['user'];
$score1 = $_POST['score1'];
$score2 = $_POST['score2'];

$str1 = $user;
$str2 = $score1;
$str3 = $score2;

if(strlen(str1, str2, str3 = 0)) //check the amount of chars in $user, $score1 and $score2
{
echo "You missed some information"; //your error if there is no info entered
}
else
{
$message = "Craig ....
$score1
$score2";
mail(craig.howarth@toon-army.co.uk, Predictions, $message, From: $user,)
}

if (mail(craig.howarth@toon-army.co.uk, Predictions, $message, From: $user,))
{
echo " Your predictions have been sent successfully";
}
else
{
echo "An error occured please go back and try again";
}
?>

Positive rep appreciated :P

Fumigator
11-07-2006, 05:34 AM
if(strlen(str1, str2, str3 = 0))


You really think that will work?

rafiki
11-07-2006, 05:48 PM
cant you check multiple variables within the 1 function?

Fumigator
11-08-2006, 01:52 AM
Don't you think you'd want to find out for sure before you post the code as a solution? :D

rafiki
11-08-2006, 03:27 AM
i was in bed when i write the script couldnt get php.net on my fone and couldnt be bothered to google it lol, sorry



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum