Jump to content

If/Elseif/Else Problems!


Spartan 117

Recommended Posts

Hello, I am making a email form and I want just one page to be able to send an email to multiple users. So I am using one of my most used and favorite things in php, query string. I want it to determine who to mail to from the url. If the url is "www.somesite.com/contact.php?jack" I would like an email to be sent to Jack's email address. So here is some code:

 

<?php $Contact_To = $_SERVER['QUERY_STRING'];
if ($Contact_To == 'bob') {
  $Contact_To == '[email protected]'; //Bob's Email...
   } elseif ($Contact_To == 'jack') {
   $Contact_To == '[email protected]'; //Jack's Email...
   } elseif ($Contact_To == 'jill') {
   $Contact_To == '[email protected]'; //Jill's Email...
   } elseif ($Contact_To == 'bill') {
   $Contact_To == '[email protected]'; //Bill's Email...
   } elseif ($Contact_To == 'jim') {
   $Contact_To == '[email protected]'; //Jim's Email...
   } elseif ($Contact_To == 'john') {
   $Contact_To == '[email protected]'; //John's Email...
   } else {
   $Contact_To == '[email protected]';
   }
   
$email_to = $Contact_To

 

If no name is specified from query string I would like it to send to the admin.

 

I have a problem with the above code and I have no clue how to fix it. This is what I get:

Parse error: parse error, unexpected T_FUNCTION in /mnt/w0000/d07/s24/b02a01ae/www/contactus.php on line 30

 

I had no error and it was working 100% until I changed it to email anyone from the string. (This: $email_to = [email protected] was all by itself and it worked perfect) Could somebody please help me out?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/38858-ifelseifelse-problems/
Share on other sites

<?php $Contact_To = $_SERVER['QUERY_STRING'];
if ($Contact_To == 'bob') {
  $Contact_To == '[email protected]'; //Bob's Email...
   } elseif ($Contact_To == 'jack') {
   $Contact_To == '[email protected]'; //Jack's Email...
   } elseif ($Contact_To == 'jill') {
   $Contact_To == '[email protected]'; //Jill's Email...
   } elseif ($Contact_To == 'bill') {
   $Contact_To == '[email protected]'; //Bill's Email...
   } elseif ($Contact_To == 'jim') {
   $Contact_To == '[email protected]'; //Jim's Email...
   } elseif ($Contact_To == 'john') {
   $Contact_To == '[email protected]'; //John's Email...
   } else {
   $Contact_To == '[email protected]';
   }
   
$email_to = $Contact_To

 

try that

Link to comment
https://forums.phpfreaks.com/topic/38858-ifelseifelse-problems/#findComment-186872
Share on other sites

I have no idea what a switch statement is. I will go look it up. But It appears that there is a small problem with my contact page. It always sends an email to the admin.

 

This is the entire code:

 

<html>
<head><title>Contact EvG¬Clan</title></head>
<body>
<?php $Contact_To = $_SERVER['QUERY_STRING'];
if ($Contact_To == 'bob') {
  $Contact_To = '[email protected]'; //Bob's Email...
   } elseif ($Contact_To == 'jack') {
   $Contact_To = '[email protected]'; //Jack's Email...
   } elseif ($Contact_To == 'jill') {
   $Contact_To = '[email protected]'; //Jill's Email...
   } elseif ($Contact_To == 'bill') {
   $Contact_To = '[email protected]'; //Bill's Email...
   } elseif ($Contact_To == 'jim') {
   $Contact_To = '[email protected]'; //Jim's Email...
   } elseif ($Contact_To == 'john') {
   $Contact_To = '[email protected]'; //John's Email...
   } else {
   $Contact_To = '[email protected]';
   }
   
$email_to = $Contact_To;
// now this bit is a little bit clever it ASCII encodes
// the above email addresss for a link. (@ LINE 102)
// It's to help stop spambots from harvesting your email address.
function ascii_encode($str){
global $encoded;
$encoded="";
for ($i=0; $i < strlen($str); $i++){
$encoded .= '&#'.ord(substr($str,$i)).';';
}
return;
}

// this function just displays the errors, you need a
// class ".err" in the css for it to highlight properly.
// I've put a little something in above...

function err_display($err_array, $err){
   if ($err_array["$err"]){
      print ("<tr class=err>");
   }else{
      print ("<tr>");
   }
}

if ($submit){


      $err_count = count($err_array);
      if ($err_count != 0){
         print ("<p class=err>Please correct the $err_count error(s):");
            while (list($index,$value) = each($err_array)){
               print ("$value<br>");
            }
         print ("</p>");
      }else{
         reset($HTTP_POST_VARS);
         while (list($key,$val) = each($HTTP_POST_VARS)){
            $message .= "$key: $val\n";
            print ("$key: $val<br>");
         }

         $email_headers = "From: Possible Future Member\n";//<$email>\n";
         $email_subject = "Application";
         
         // this sends the email using the standard php mailer (sendmail?)
         @mail($email_to, $email_subject, $message, $email_headers);
         
         // Here's your thank you note...
         print ("<h2>Thank you $real_name!</h2>");
      }
   }


if (!$submit OR $err_count != "0"){
   ascii_encode("$email_to");
   print ("<p>Please use the form to send an email to Us</p>");   
   
   print ("<form action=\"contact.php\" method=\"POST\">");
   print ("<table>\n<tr>");
   print ("<td>Name:</td><td><input type=\"text\" size=14 size=14 name=\"Name\" value=\"$real_name\"></td></tr>\n<tr>");   
   print ("<tr>");
   print ("<tr><td>Email:</td><td><input type=\"text\" name=\"Email\" value=\"$e_mail\"></td></tr>\n");
   print ("<tr><td>Message:</td><td><textarea name=\"textarea\" cols=\"40\" rows=\"5\" name=\"Message\" value=\"$the_message\"</textarea></td></tr>\n");
   print ("<tr><td> </td><td><input name=\"submit\" type=\"Submit\" value=\"Send\"></td></tr>\n");
   print ("</table>");
   print ("<input type=\"Hidden\" name=\"IP\" value=\"$REMOTE_ADDR\">");
   print ("</form>");
}

?>
<br>
<div align="center"><input type="button" value="Close This Window" onclick="self.close()"></div>
</body>
</html> 

 

I think it has to do with: 

print ("<form action=\"contact.php\" method=\"POST\">");

 

That needs to be the same page as I am on, with the query string I am using. But how would I do that?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/38858-ifelseifelse-problems/#findComment-186884
Share on other sites

I'm not sure why Jesi pointed out the difference between "==" and "=" above because you had it correct the first time.  The second posting of your code has the incorrect assignment operator ("=") where you want the comparison operator ("==") in your if / else statements.

 

Echo $contact_to to see what it is equal to.  Since it is assigning $email_to to the default value, $contact_to isn't equal to what you think it is or should be.

Link to comment
https://forums.phpfreaks.com/topic/38858-ifelseifelse-problems/#findComment-186892
Share on other sites

$Contact_To = $_SERVER['QUERY_STRING'];

 

What is that? I think you want $_POST['contact'] or something.

 

$Contact_To = $_SERVER['QUERY_STRING']; I do because I know no other way to do this. I just want one page to send a message to a variety of people. contactus.php?spartan117 would send a message to my email address.

Link to comment
https://forums.phpfreaks.com/topic/38858-ifelseifelse-problems/#findComment-186897
Share on other sites

I'm not sure why Jesi pointed out the difference between "==" and "=" above because you had it correct the first time.  The second posting of your code has the incorrect assignment operator ("=") where you want the comparison operator ("==") in your if / else statements.

 

Echo $contact_to to see what it is equal to.  Since it is assigning $email_to to the default value, $contact_to isn't equal to what you think it is or should be.

 

The first code has this:

if ($Contact_To == 'bob') {
  $Contact_To == '[email protected]'; //Bob's Email...
   } elseif ($Contact_To == 'jack') {
   $Contact_To == '[email protected]'; //Jack's Email...
   } elseif ($Contact_To == 'jill') {

 

Which is not correct - he uses == everywhere.

Link to comment
https://forums.phpfreaks.com/topic/38858-ifelseifelse-problems/#findComment-186900
Share on other sites

I think I know how to fix it but I don't know how make something = something AND something... lol. Like this:

 

print ("<form action=\"contact.php? & $_SERVER['QUERY_STRING']\" method=\"POST\">");

 

I think this i what is doing it because when I hit send it just goes to contact.php without the ?jack

 

Link to comment
https://forums.phpfreaks.com/topic/38858-ifelseifelse-problems/#findComment-186903
Share on other sites

What I have tried was this:

 

print ("<form action=\"contactus.php?"&& $_SERVER['QUERY_STRING']" \" method=\"POST\">");

 

But I get an error "unexpected T_CONSTANT_ENCAPSED_STRING"

 

I want action to = contact.php & $_SERVER['QUERY_STRING']

 

Or maybe just the whole complete url that i am already at.

Link to comment
https://forums.phpfreaks.com/topic/38858-ifelseifelse-problems/#findComment-186909
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.