Reputation: 195
I've searched and searched and haven't found anything, though that's probably because I don't even know what's causing this bug, let alone how to fix it.
First off, I'm sort of new. I know the basics of PHP but there's a lot I don't know, so excuse me if the answer to this is simple (or if you can't read my code because it's so messy!).
I thought as one of my first applications I'd make a simple email script, where the user enters their name, subject, message, and email address.
This is the relevant bit of the form page: http://pastebin.com/UhQukUuB (sorry, don't quite know how to embed code...).
<form action="send.php" method="post">
Name: <input type="text" name="name" size="20" /><br />
Subject: <input type="text" name="subject" size="20" /><br />
Message:<br /><textarea name="message" rows="12" cols="55"></textarea><br />
Your email address: <input type="text" name="emailAddress" size="20" /><br />
<input type="submit" value="Send" />
</form>
This is in send.php: http://pastebin.com/nky0L1dT .
<?php
$name=$_POST['name'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$emailAddress=$_POST['emailAddress'];
//It's receiving the variables correctly, I've checked by printing the variables.
$errors=array(); //Creates empty array with 0 indexes. This will now be filled with error messages (if there are any errors).
if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
if($name==""){
$errors[0]="You did not supply a name.";
}
if($subject==""){
$errors[count($errors)]="You did not supply a subject."; //I'm using count($errors) so it will create a new index at the end of the array, regardless of how many indexes it currently has (if that makes sense, it's hard to explain)
}
if($message==""){
$errors[count($errors)]="You did not supply a message.";
}
if($emailAddress==""){
$errors[count($errors)]="You did not supply an email address.";
}
}
//Were there any errors?
if(!count($errors)==0){
print "The following errors were found:<br />";
for($i=0; $i<count($errors); $i++){
print $errors[$i]."<br />";
}
die ();
}
//Rest of email script, which I'll write when the stupid bug is fixed. :(
?>
Here's what happens: when you miss out the name, subject, or message, the error detector works fine and displays "You did not supply a name/subject/message". When you miss out the email address, nothing happens. I know it's being stored in the array and being received correctly, because if you miss out both the name/subject/message AND the email address, it displays "You did not supply a name/subject/message. You did not supply an email address". I've been staring at my screen for half an hour now just trying to spot why it's doing this?
Thanks.
Upvotes: 1
Views: 120
Reputation:
There are two problems, one of which is in your negation here:
if(!count($errors)==0){
The unary !
applies to count($errors)
, not count($errors)==0
. Use !=
instead:
if(count($errors) != 0) {
The second error is the use of an assignment (=
) as opposed to a comparison (==
) here:
if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
As a side note, you do not need to use $errors[count($errors)]
to add an item to the end of an array. $errors[]
will do. For iteration, it is also much better to use a foreach
loop than what you are currently doing.
Upvotes: 1
Reputation: 3024
In your if statement, you are using assignment, not comparison
if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
Instead of
if($name=="" || $subject=="" || $message=="" || $emailAddress==""){
Upvotes: 0
Reputation: 35370
Change
if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
to
if($name=="" || $subject=="" || $message=="" || $emailAddress==""){
You're inadvertently setting $emailAddress
to ""
in the if
statement
... || $emailAddress=""){
Upvotes: 0