TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   else problem (http://www.talkphp.com/absolute-beginners/3591-else-problem.html)

9three 11-11-2008 04:52 PM

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...

tony 11-11-2008 05:24 PM

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

xenon 11-11-2008 05:25 PM

yeah, you should TEST the conditions, and not make assignments. therefore...

PHP Code:

if($a 'b'

will become:

PHP Code:

if($a == 'b'

Also, you should use the OR operand ( || ) instead of the keyword or, because it has greater precedence.

9three 11-11-2008 05:51 PM

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

Runar 11-11-2008 06:02 PM

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
   
}



9three 11-11-2008 06:36 PM

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.

Runar 11-11-2008 06:47 PM

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$_POSTTRUE );
    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>


9three 11-11-2008 06:57 PM

nope its not working !empty will still send it out :(

Runar 11-11-2008 07:00 PM

Quote:

Originally Posted by 9three (Post 19538)
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?

9three 11-11-2008 07:20 PM

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");
}

?>


Runar 11-11-2008 07:58 PM

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' );
}

?>


9three 11-11-2008 08:40 PM

I guess I was confused with the whole if {} thing. Yes it works now :) thanks

Runar 11-11-2008 09:12 PM

I am glad I could help! :)

EyeDentify 11-12-2008 11:04 AM

@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.

9three 11-12-2008 10:32 PM

yea i have = and == covered :D


All times are GMT. The time now is 09:28 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0