PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 12:33 PM
Smurff289 Smurff289 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 37 Smurff289 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 58 sec
Reputation Power: 1
IF Statement PHP form

I have a form that sends to an email I specify.

Ex:
PHP Code:
 mail"myemail@.mydomain.com",
"My Site | Contact me Form",
$message,"From: $email"); 


However, I want to add a drop down that asks "Why are you contacting me today" and the fields will be different. And depending on which answer they choose it will send to that person. The easy way to do this is just put the email in the Value field. However, than I won't know which one they choose, when the recipient opens it.

Ex. Why Did You Contact us Today?
I was bored - (value=Jane@mydomain.com
Dog Ate My Homework - (Value=Mike@mydomain.com)
I Like pizza. - (Value=Jane@mydomain.com)

Now The issue is... When Jane gets the email she won't know whether the contact form is referencing "I was Bored" or "I Like Cheeseburgers"

Can I use a IF Statement inside a PHP form? How?

I tried...
PHP Code:
 $emailto $subj
if ( 
$subj == "I was Bored" ) { echo "Jane@mydomain.com"; }
if ( 
$subj == "Dog Ate My Homework" ) { echo "Mike@mydomain.com"; }
if ( 
$subj == "I Like pizza." ) { echo "Jane@mydomain.com"; } 

Reply With Quote
  #2  
Old November 5th, 2012, 12:40 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
Its not clear to me what you are really asking. In your script you have the index. Why can't you use that to assign the desired subject and recipient? It sounds like you can set up an array of subjects and recipients, and use the index from the drop down as the index on those arrays to create the email.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #3  
Old November 5th, 2012, 12:54 PM
Smurff289 Smurff289 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 37 Smurff289 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 58 sec
Reputation Power: 1
Quote:
Originally Posted by gw1500se
Its not clear to me what you are really asking. In your script you have the index. Why can't you use that to assign the desired subject and recipient? It sounds like you can set up an array of subjects and recipients, and use the index from the drop down as the index on those arrays to create the email.


That sounds exactly like what I want to do. I just don't know how to get there. I want the recipient to change based on the drop down chosen. Can You please provide a brief example? Maybe a link if possible?

Reply With Quote
  #4  
Old November 5th, 2012, 01:04 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
I don't have a specific example or link for you but I can explain what to do and you can post what you came up with if you are struggling with the details. Create 2 arrays, one containing the subject texts and the other the corresponding recipients. Generate your option tags using the array index as the value and the corresponding subject as the text. When the form is posted, the value of the select array in $_POST will be the desired index. Use that to set the subject and recipient from the arrays when you generate the email.

Reply With Quote
  #5  
Old November 5th, 2012, 02:32 PM
badger_fruit's Avatar
badger_fruit badger_fruit is offline
Confused badger
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Location: West Yorkshire
Posts: 760 badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
To the OP, your IF statement code was almost right .... please see my changes below which should make it work.

PHP Code:
if ( $subj == "I was Bored" ) { $emailto "Jane@mydomain.com"; }
if ( 
$subj == "Dog Ate My Homework" ) { $emailto "Mike@mydomain.com"; }
if ( 
$subj == "I Like pizza." ) { $emailto "Jane@mydomain.com"; } 


Another method would be with a SWITCH statement:-

PHP Code:
switch ($subj) {
  case 
"I was Bored":
    
$emailto "Jane@mydomain.com";
    break;
  case 
"Dog Ate My Homework":
    
$emailto "Mike@mydomain.com";
    break;
  case 
"I Like pizza.":
    
$emailto "Jane@mydomain.com";
    break;

  default:
    
$emailto "none_of_the_above@mydomain.com";
    break;




The switch statement would allow you to have an extra, "default" option - just in case the user's selection was "none of the above" - you can see this in action with the "default" option shown in the example above.
__________________
The number for UK Emergencies is changing, the new number is 0118 999 881 999 119 7253

"For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
- George Orwell, 1984

Reply With Quote
  #6  
Old November 5th, 2012, 03:09 PM
Smurff289 Smurff289 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 37 Smurff289 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 58 sec
Reputation Power: 1
Quote:
Originally Posted by badger_fruit
To the OP, your IF statement code was almost right .... please see my changes below which should make it work.

PHP Code:
if ( $subj == "I was Bored" ) { $emailto "Jane@mydomain.com"; }
if ( 
$subj == "Dog Ate My Homework" ) { $emailto "Mike@mydomain.com"; }
if ( 
$subj == "I Like pizza." ) { $emailto "Jane@mydomain.com"; } 
...

I made the changes you suggested, I'm not getting an error page now, but the email is not coming through. This is the entire form...

PHP Code:
<?php
 
$subj 
$emailto
if ( 
$subj == "I was Bored" ) { $emailto "Jane@mydomain.com"; } 
if ( 
$subj == "Dog Ate My Homework" ) { $emailto "Mike@mydomain.com"; } 
if ( 
$subj == "I Like pizza" ) { $emailto "Jane@mydomain.com"; }
$subj $_REQUEST['subj'] ; 
$name $_REQUEST['name'] ; 
$email $_REQUEST['email']; 
$how $_REQUEST['how'] ; 
$comments $_REQUEST['comments'] ; 


$message 
Contact Information: 
Name: 
$name 
Email: 
$email 
How did you hear about us? 
$how 
Subject: 
$subj 
Comments: 
$comments";
 
mail"$emailto","Contact Us Form",$message,"From: $email"); 
header"Location: success.html" ); ?>

Reply With Quote
  #7  
Old November 5th, 2012, 03:19 PM
badger_fruit's Avatar
badger_fruit badger_fruit is offline
Confused badger
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Location: West Yorkshire
Posts: 760 badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
mail( "$emailto","Contact Us Form",$message,"From: $email");

Again, just a few changes ...

mail($emailto, "Contact Us Form", $message, "From: {$email}");

Note I have removed the quotes around $emailto, they're not needed and I have enclosed the $email variable within curly brackets. You don't have to do this, you could also write it like:

mail($emailto, "Contact Us Form", $message, "From: " . $email);

(I added spaced just to make it easier to read)

If you make those changes and you're still not able to send/receive email, check that everything is being populated correctly (i.e. echo the variables to screen) and if they are, then check your logs (e.g. /var/log/apache2/error.log).


Oh, also, get rid of the line:-
$subj = $emailto;

and replace $_REQUEST with either $_GET or $_POST (depends on what your form uses, if you're not sure, use print_r($_GET) or print_r($_POST) somewhere in the script and use whichever one has data)

Reply With Quote
  #8  
Old November 5th, 2012, 03:37 PM
Smurff289 Smurff289 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 37 Smurff289 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 58 sec
Reputation Power: 1
Quote:
Originally Posted by badger_fruit
mail( "$emailto","Contact Us Form",$message,"From: $email");

Again, just a few changes ...

mail($emailto, "Contact Us Form", $message, "From: {$email}");

Note I have removed the quotes around $emailto, they're not needed and I have enclosed the $email variable within curly brackets. You don't have to do this, you could also write it like:

mail($emailto, "Contact Us Form", $message, "From: " . $email);

(I added spaced just to make it easier to read)

If you make those changes and you're still not able to send/receive email, check that everything is being populated correctly (i.e. echo the variables to screen) and if they are, then check your logs (e.g. /var/log/apache2/error.log).


Oh, also, get rid of the line:-
$subj = $emailto;

and replace $_REQUEST with either $_GET or $_POST (depends on what your form uses, if you're not sure, use print_r($_GET) or print_r($_POST) somewhere in the script and use whichever one has data)


I just printed to screen and I get all the variables EXCEPT the $emailto

Reply With Quote
  #9  
Old November 5th, 2012, 03:40 PM
badger_fruit's Avatar
badger_fruit badger_fruit is offline
Confused badger
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Location: West Yorkshire
Posts: 760 badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
Quote:
Originally Posted by Smurff289
I just printed to screen and I get all the variables EXCEPT the $emailto


When you're performing the comparison you have to ensure that what you're checking against is exact.

For example,

"Value" is different to "value"

If you use the 'switch' code snippet I gave you earlier, does $emailto get set to "none_of_the_above@mydomain.com"?

If it does, check your checks!

Reply With Quote
  #10  
Old November 5th, 2012, 03:44 PM
Smurff289 Smurff289 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 37 Smurff289 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 58 sec
Reputation Power: 1
Quote:
Originally Posted by badger_fruit
When you're performing the comparison you have to ensure that what you're checking against is exact.

For example,

"Value" is different to "value"

If you use the 'switch' code snippet I gave you earlier, does $emailto get set to "none_of_the_above@mydomain.com"?

If it does, check your checks!

I know. I didnt notice the last line about removing $subj = ... Im doing that now and testing.

Reply With Quote
  #11  
Old November 5th, 2012, 03:54 PM
badger_fruit's Avatar
badger_fruit badger_fruit is offline
Confused badger
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Location: West Yorkshire
Posts: 760 badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
Quote:
Originally Posted by Smurff289
I know. I didnt notice the last line about removing $subj = ... Im doing that now and testing.


That shouldn't really affect anything as you're setting it ....

$subj = $emailto;

Then in the next line(s), setting it to something else.

If $emailto is empty then the IF statements are either wrong (the string you're comparing it to may be different, even by a single character). Hence, if you use the switch statement instead, you have at least a "catch all" setting so if there's no exact match, at least $emailto will be set and should then send the email.

Reply With Quote
  #12  
Old November 5th, 2012, 04:06 PM
Smurff289 Smurff289 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 37 Smurff289 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 58 sec
Reputation Power: 1
I did as you said and used the Switch Method. When I printed it to the screen all fields populate except the $emailto. The $subj field populates just fine. So we know that the value provided is valid. It just doesn't read it? I would check error logs, but I am in a shared hosting environment.

This is the section of the form
PHP Code:
<label
<
select name="subj" id="subj"
<
option value="Online" selected="selected">Online</option>
<
option value="Facebook">Facebook</option
</
select
</
label

This is the PHP Form
PHP Code:
switch ($subj) { 
case 
"Online"$emailto "Jane@mydomain.com"
break; 
case 
"Facebook"$emailto "Mike@mydomain.com"
break; 
default: 
$emailto "none_of_the_above@mydomain.com"
break; 

$subj $_POST['subj'] ; 
$name $_POST['name'] ; 
$email $_POST['email']; 
$how $_POST['how'] ; 
$comments $_POST['comments'] ; 

Reply With Quote
  #13  
Old November 5th, 2012, 04:10 PM
badger_fruit's Avatar
badger_fruit badger_fruit is offline
Confused badger
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Location: West Yorkshire
Posts: 760 badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
You're assigning the variable AFTER doing a check:-

PHP Code:
switch ($subj) { 
case 
"Online"$emailto "Jane@mydomain.com"
break; 
case 
"Facebook"$emailto "Mike@mydomain.com"
break; 
default: 
$emailto "none_of_the_above@mydomain.com"
break; 

$subj $_POST['subj'] ; 


Should be:

PHP Code:
 $subj $_POST['subj'] ;
switch (
$subj) { 
case 
"Online"$emailto "Jane@mydomain.com"
break; 
case 
"Facebook"$emailto "Mike@mydomain.com"
break; 
default: 
$emailto "none_of_the_above@mydomain.com"
break; 



Comments on this post
Smurff289 agrees!

Reply With Quote
  #14  
Old November 5th, 2012, 04:20 PM
Smurff289 Smurff289 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 37 Smurff289 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 58 sec
Reputation Power: 1
Ermahgerd It Works!!!!

You're My Hero!!

The If Statements Work Now Too!!!!!

Reply With Quote
  #15  
Old November 5th, 2012, 04:24 PM
badger_fruit's Avatar
badger_fruit badger_fruit is offline
Confused badger
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Location: West Yorkshire
Posts: 760 badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
lol, ermagerd cherkern nergets
anyway, yeah, no problem at all, you're welcome!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > IF Statement PHP form

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap