The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
IF Statement PHP form
Discuss IF Statement PHP form in the PHP Development forum on Dev Shed. IF Statement PHP form PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 5th, 2012, 12:33 PM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 37
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"; }
|

November 5th, 2012, 12:40 PM
|
|
|
|
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.
|

November 5th, 2012, 12:54 PM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 37
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?
|

November 5th, 2012, 01:04 PM
|
|
|
|
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.
|

November 5th, 2012, 02:32 PM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
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
|

November 5th, 2012, 03:09 PM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 37
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" ); ?>
|

November 5th, 2012, 03:19 PM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
|
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)
|

November 5th, 2012, 03:37 PM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 37
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
|

November 5th, 2012, 03:40 PM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
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!
|

November 5th, 2012, 03:44 PM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 37
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.
|

November 5th, 2012, 03:54 PM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
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.
|

November 5th, 2012, 04:06 PM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 37
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'] ;
|

November 5th, 2012, 04:10 PM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
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;
}

|

November 5th, 2012, 04:20 PM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 37
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!!!!!
|

November 5th, 2012, 04:24 PM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
|
lol, ermagerd cherkern nergets
anyway, yeah, no problem at all, you're welcome!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|