 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
11-11-2008, 03:52 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Posts: 75
Thanks: 4
|
else problem
Anyone detect the problem here?
PHP Code:
if ($name = '' or $phone ='' or $email = '' or $message = ''){ header("Location: http://www.url.com/error.php"); } else { (mail($recipient, $subject, $body, $header)) header("Location: http://www.url.com/thanks.php"); }
I'm getting an error in the 2nd header...
|
|
|
|
11-11-2008, 04:24 PM
|
#2 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
I am not sure now, what could be. can you tell us what type of error do you get?
also restructuring a little the code:
PHP Code:
if (is_null($name) || is_null($phone) || is_null($email) || is_null($message)){
header("Location: http://www.url.com/error.php");
} else {
if(mail($recipient, $subject, $body, $header))
header("Location: http://www.url.com/thanks.php");
}
that might help, maybe the error carry over from the mail function being inside parenthesis
|
|
|
|
11-11-2008, 04:25 PM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
yeah, you should TEST the conditions, and not make assignments. therefore...
will become:
Also, you should use the OR operand ( || ) instead of the keyword or, because it has greater precedence.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
11-11-2008, 04:51 PM
|
#4 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Posts: 75
Thanks: 4
|
I changed to || instead of OR. I also added the IF inside the {} and it fixed it.
I changed the code to
PHP Code:
if (is_null($_POST['name']) || is_null($_POST['phone']) || is_null($_POST['email']) || is_null($_POST['message'])){
header("Location: http://www.url.com/error.php");
} else { if (mail($recipient, $subject, $body, $header))
header("Location: http://www.url.com/thanks.php");
}
The email is sent out whether they fill it out or not. And it always sends the sender to thanks.php instead of error.php when its null
|
|
|
|
11-11-2008, 05:02 PM
|
#5 (permalink)
|
|
The Contributor
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
|
It would be better to do the last part this way:
PHP Code:
elseif( mail( $recipient, $subject, $body, $header ) ) { header( 'Location: http://www.url.com/thanks.php' ); }
Unless you are going to do something like the following:
PHP Code:
if( your first if ) { // some stuff } else { if( mail( $recipient, $subject, $body, $header ) ) { header( 'Location: http://www.url.com/thanks.php' ); }
else { // failed to send mail } }
|
|
|
11-11-2008, 05:36 PM
|
#6 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Posts: 75
Thanks: 4
|
the top part of my code is just assigning variables to my form.
I tried out the first code you gave me
PHP Code:
elseif( mail( $recipient, $subject, $body, $header ) ) { header( 'Location: http://www.url.com/thanks.php' ); }
it sends me straight to my thanks.php whether its null or not.
The 2nd code you put is what tony suggested. which i tried and it sent me to thanks.php whether null or not.
|
|
|
|
11-11-2008, 05:47 PM
|
#7 (permalink)
|
|
The Contributor
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
|
I know. The mail() function will actually return true (or attempt to send the mail, if I understood it correctly) even if the parameters submitted are 0 or not valid at all.
I would personally use empty() instead of is_null(), since is_null() will return false even if the variable is '' or 0.
PHP Code:
if( !empty( $_POST['subject'] ) && !empty( $_POST['message'] ) )
{
if( mail( 'my@mail.com', $_POST['subject'], $_POST['message'] ) )
{
// Success!
}
else
{
// Failure!
}
}
else
{
// One or more of the required fields are not set
}
Edit: Try to run the following code, then submit the form without filling any fields, and you will see why using is_null() is a bad idea in such cases:
PHP Code:
<?php
if( isset( $_POST['submit'] ) )
{
$r = print_r( $_POST, TRUE );
echo '<pre>' . $r . '</pre>';
if( is_null( $_POST['number_1'] ) )
{
echo 'is_null';
}
}
?>
<form action="/test.php" method="post">
<input type="text" name="number_1" />
<input type="text" name="number_2" />
<button type="submit" name="submit">Submit</button>
</form>
|
|
|
11-11-2008, 05:57 PM
|
#8 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Posts: 75
Thanks: 4
|
nope its not working !empty will still send it out :(
|
|
|
|
11-11-2008, 06:00 PM
|
#9 (permalink)
|
|
The Contributor
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
|
Quote:
Originally Posted by 9three
nope its not working !empty will still send it out :(
|
Then you are doing something wrong when setting the variables. Could you post your entire script, at least the parts where you are setting the variables?
|
|
|
|
The Following User Says Thank You to Runar For This Useful Post:
|
|
11-11-2008, 06:20 PM
|
#10 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Posts: 75
Thanks: 4
|
PHP Code:
<?php
$name = $_POST['name']; //senders name $phone = $_POST['phone']; $website = $_POST['website']; $email = $_POST['email']; //senders e-mail adress $recipient = "info@url.com"; //recipient $message = $_POST['message']; //mail body $subject = "Client Request"; //subject $header = "From: ". $name . " <" . $email . ">\r\n"; //optional headerfields $body = "Name: ". $name ."
Phone: ".$phone." Website: ".$website." Email:".$email." Message: ".$message."\r\n"; if ( !empty($_POST['name']) || !empty($_POST['phone']) || !empty($_POST['email']) || !empty($_POST['message'])){ header("Location: http://www.url.com/error.php"); } else { if (mail($recipient, $subject, $body, $header)) header("Location: http://www.url.com/thanks.php"); }
?>
|
|
|
|
11-11-2008, 06:58 PM
|
#11 (permalink)
|
|
The Contributor
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
|
Try this code. Either was my explanation poor, or you misunderstood what I meant about using !empty(). Anyway, try this code and let me know what you get:
PHP Code:
<?php
// Display all errors ini_set( 'display_errors', 'yes' );
// Post data $name = $_POST['name']; $phone = $_POST['phone']; $website = $_POST['website']; $email = $_POST['email']; $message = $_POST['message'];
// Define some mail variables $recipient = "info@url.com"; $subject = 'Client Request'; $header = 'From: ' . $name . ' <' . $email . '>' . "\r\n"; $body = 'Name: ' . $name . "\n\nPhone: " . $phone . "\n\nWebsite: " . $website . "\n\nEmail: " . $email . "\n\nMessage: " . $message;
// Check that all required fields (name, phone, email, message) are set if( !empty( $name ) && !empty( $phone ) && !empty( $email ) && !empty( $message ) ) { // Attempt to send mail if( mail( $recipient, $subject, $body, $header ) ) { header( 'Location: /thanks.php' ); } // Failed else { header( 'Location: /mailfail.php' ); } }
// One or more of the required fields were missing, redirect to error page else { header( 'Location: /error.php' ); }
?>
|
|
|
11-11-2008, 07:40 PM
|
#12 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Posts: 75
Thanks: 4
|
I guess I was confused with the whole if {} thing. Yes it works now :) thanks
|
|
|
|
11-11-2008, 08:12 PM
|
#13 (permalink)
|
|
The Contributor
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
|
I am glad I could help! :)
|
|
|
11-12-2008, 10:04 AM
|
#14 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
|
@9three
From this thread i never grasped if you understood the difference between:
Assigning:
PHP Code:
<?PHP $myVar = 'myString'; ?>
And comparison:
PHP Code:
<?PHP $myVar == 'myString'; ?>
In the first you assign the string to $myVar
In the last you check to see if the $myVar has the same value as the string.
Itīs an easy misstake, i do it often myself.
Anyways :) good luck.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
|
|
|
|
11-12-2008, 09:32 PM
|
#15 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Posts: 75
Thanks: 4
|
yea i have = and == covered :D
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|