Forum Moderators: coopster

Message Too Old, No Replies

admin email variable - working but else echo's

the "if" is carried out but the "else" auto echo's (false)

         

php4U

10:20 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



I originally had code that would submit the form info into the database and email the admin. Then I wanted to be able to let the admin have the option so they could set if they wished to receive an email each time. Both code pieces are in the same page. One sends an email and the other doesn't. My problem is the code in bold.

#### NO PROBLEMS WITH THIS
$admin_mail="2"; // when set to 1 the following works fine, but if 2 the bold code executes

if ($admin_mail == "1") {

if (mail($recipient,$subject,$message,$headers)) {
echo "<p>Thank you! Your testimonial has been successfully submitted.</p>";
include("config.php");
$connection = mysql_connect("$host","$username","$password")or die("cannot connect");
$db = mysql_select_db("$db_name")or die("cannot select DB");

$sql="INSERT INTO $tbl_name(id, value, value1, value2, value3)VALUES('NULL','$value', '$value1', '$value2', '$value3')";
$result = mysql_query($sql);

} else {
echo "<p>Your testimonial could not be sent at this time.</p>";
}
}
}
}
####

When $admin_mail="2"; the following works, but the text in the ELSE statement shows without an error.

####
if ($admin_mail == "2") {
echo "<p>Thank you! Your testimonial has been successfully submitted.</p>";
include("config.php");
$connection = mysql_connect("$host","$username","$password")or die("cannot connect");
$db = mysql_select_db("$db_name")or die("cannot select DB");

$sql="INSERT INTO $tbl_name(id, value, value1, value2, value3)VALUES('NULL','$value', '$value1', '$value2', '$value3')";
$result = mysql_query($sql);

} else {
echo "This is what is appearing when you visit the form, but shouldn't unless there is an error.";
}

####

I am missing something with the code in bold because it submits the info, but whatever is in the "else" statement shows before the form is submitted, and I want to show an error like the code at the top that works. It did this when I removed the mail() statement...so maybe with one less condition it is throwing a "false" automatically. Thank you for any help.

php4U

4:53 am on Nov 25, 2007 (gmt 0)

10+ Year Member



I'm sure this is not the best way to do it, but I added the following to the top of what I had. This allows the echo to remain after the ELSE without returning false, and showing the text at the beginning.

function no_mail($no_mail) { echo ""; }

if ($admin_mail == "2") {

if ($no_mail == $no_mail) {

jatar_k

2:15 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my first thought is that I would think that admin_mail can be 1 or 2, not both, so these 2 code pieces should be dependent

use an if else or a switch, probably a switch with a default case that shows the error

php4U

3:29 am on Nov 26, 2007 (gmt 0)

10+ Year Member



You're right jatar_k either $admin_mail="1"; or $admin_mail="2"; will be set in the config file by the user. So only one portion of code will execute. I might try to re-write it so the mail function is at the bottom with the admin_mail var above it. That way it will enter everything into the database and then determine if an email should be sent. That should condense it down into one set of code rather than having code for each.