#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    3

    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"; } 
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    3
    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?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,161
    Rep Power
    492
    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.
    "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
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    3
    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" ); ?>
  12. #7
  13. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,161
    Rep Power
    492
    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)
    "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
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    3
    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
  16. #9
  17. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,161
    Rep Power
    492
    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!
    "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
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    3
    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.
  20. #11
  21. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,161
    Rep Power
    492
    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.
    "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
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    3
    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'] ; 
  24. #13
  25. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,161
    Rep Power
    492
    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
    "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
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    3
    Ermahgerd It Works!!!!

    You're My Hero!!

    The If Statements Work Now Too!!!!!
  28. #15
  29. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,161
    Rep Power
    492
    lol, ermagerd cherkern nergets
    anyway, yeah, no problem at all, you're welcome!
    "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

IMN logo majestic logo threadwatch logo seochat tools logo