PHP conditional statement/operator question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neonfish07
Forum Newbie
Posts: 4
Joined: Thu Mar 13, 2008 5:23 pm

PHP conditional statement/operator question

Post by neonfish07 »

I'm trying to make a form generate an error if BOTH the email and phone fields are empty (at least one contact field must be filled out).

Why doesn't this code work? If I enter an email but not a phone number, the error is displayed...

Code: Select all

 
<?php 
 
if($visitormail=="" && $phone==""){
echo "<p class='content'><b>Error</b></p><p class='content'>Please <a href='javascript&#058;history.go(-1)'>go back</a> and enter an e-mail address or phone number so we can contact you.";
}
 
?>
Thx for helping the noob 8)
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: PHP conditional statement/operator question

Post by nowaydown1 »

Your conditional seems ok to me, so I would guess that the problem is occuring elsewhere in your script. I see you're explicitly checking variable names, are you using register_globals? If you could post the rest of your code we could probably spot the issue. Thanks! :)
neonfish07
Forum Newbie
Posts: 4
Joined: Thu Mar 13, 2008 5:23 pm

Re: PHP conditional statement/operator question

Post by neonfish07 »

Hey nowaydown, thanks for the reply :D Yes, register_globals is on.

Here is the whole script:

Code: Select all

<?php
include('header.php');
?>
<div id="content-about">
<?php
$visitormail = $_POST['visitormail'];
$visitor = $_POST['visitor'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
$ip = $_POST['ip'];
$subject = $_POST['subject'];
$SpamErrorMessage = "<p class='content'><b>Error</b></p><p class='content'>Please <a href='javascript&#058;history.go(-1)'>go back</a> and remove all website addresses from your submission (this helps us prevent spam).</p>\n";
 
if (preg_match("/http/i", "$visitor")) {echo "$SpamErrorMessage"; exit();} 
if (preg_match("/http/i", "$attn")) {echo "$SpamErrorMessage"; exit();} 
if (preg_match("/http/i", "$notes")) {echo "$SpamErrorMessage"; exit();} 
if (preg_match("/http/i", "$subject")) {echo "$SpamErrorMessage"; exit();} 
 
if(($visitormail=="") && ($attn=="")){
    echo "<p class='content'><b>Error</b></p><p class='content'>Please <a href='javascript&#058;history.go(-1)'>go back</a> and enter an e-mail address or phone number so we can contact you.";
    }
 
elseif (!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "<p class='content'><b>Error</b></p><p class='content'>Please <a href='javascript&#058;history.go(-1)'>go back</a> and enter a 
 
valid e-mail address.</p>\n";
} elseif(empty($visitor) || empty($notes )) {
echo "<p class='content'><b>Error</b></p><p class='content'>Please <a href='javascript&#058;history.go(-1)'>go back</a> and fill in all 
 
fields.</h2>\n";
} else {
 
$todayis = date("l, F j, Y, g:i a") ;
 
$attn = $attn ;
$subject = $subject;
 
$notes = stripcslashes($notes);
 
$message = "From: $visitor ($visitormail)\n
Phone Number: $attn \n
Date: $todayis GMT \n
Subject: $subject\n
--------------------------\n
Message:\n
$notes \n
";
 
$from = "From: $visitormail\r\n";
 
mail("email@email.org", "$subject (from your website)", $message, $from);
 
?>
 
<p class="content"><img src="images/thankyou.png" alt="Thank you."/></p>
<p class='content'>Your message has been submitted and you will receive a response shortly.</p>
<a href="index.php" class='content'><p class='content'>Return to the home page</a>
</p> 
 
<?php
}
?>
</div>
<?php
include('footer.php');
?>
Hopefully my code is somewhat comprehensible :wink:

Thanks for the help <3
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: PHP conditional statement/operator question

Post by nowaydown1 »

So on the dummy form that I created, everything seems alright to me. My advice would be to dump that $_POST superglobal and take a look at what's actually coming back from the form you're using to post to this script. Just shove a:

Code: Select all

 
<?php
echo "<pre>";
print_r($_POST);
?>
 
At the of the script you posted and see what you get. I'm guessing that maybe there's a typo on a form field name on the form posting to this script or something along those lines. Let me know what you find.
neonfish07
Forum Newbie
Posts: 4
Joined: Thu Mar 13, 2008 5:23 pm

Re: PHP conditional statement/operator question

Post by neonfish07 »

nowaydown1 wrote:So on the dummy form that I created, everything seems alright to me. My advice would be to dump that $_POST superglobal and take a look at what's actually coming back from the form you're using to post to this script. Just shove a:

Code: Select all

 
<?php
echo "<pre>";
print_r($_POST);
?>
 
At the of the script you posted and see what you get. I'm guessing that maybe there's a typo on a form field name on the form posting to this script or something along those lines. Let me know what you find.
thanks again for the reply. I printed the $_POST array and this is what came up:

Code: Select all

Array
(
    [ip] => 
    [httpref] => 
    [httpagent] => 
    [visitor] => test
    [vistormail] => test@email.com
    [phone] => 789456123
    [subject] => test subject
    [notes] => test
)
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: PHP conditional statement/operator question

Post by nowaydown1 »

It looks like the element on your form is called 'vistormail', but your script is checking for 'visitormail'. Try to make them both the same and see if that doesn't do it for ya.
neonfish07
Forum Newbie
Posts: 4
Joined: Thu Mar 13, 2008 5:23 pm

Re: PHP conditional statement/operator question

Post by neonfish07 »

It works now! I never would have found that!

Thanks for your help! 8)
Post Reply