What's wrong with my code?

Discussion in 'PHP' started by Amsterdam, Jan 31, 2008.

  1. #1
    Hi,

    I am using PHP to process a contact form and send the content by email. I have added a couple of radio buttons to the form so that the user can select whether they contact the northern branch or southern branch. Unfortunately, my 'if else' statements don't seem to be doing the job properly.

    The radio buttons on the contact page have the name set as "region" with values of "north" and "south" and I am pulling the selected value into a variable called "$reg" with:

    $reg = $_POST['region'];
    Code (markup):
    Here's the code that should put the correct email address into a variable called "$to":

    if ($reg=="north") {
    		$to = "name1@domain.com <name1@domain.com>";
    			}
    	elseif ($reg=="south") {
    		$to = "name2@domain.com <name2@domain.com>";
    			}
    	else {
    		$to = "default@domain.com <default@domain.com>";
    	}
    Code (markup):
    Unfortunately, regardless of which radio button is selected the mail keeps being sent to the default address. Where am I going wrong?

    Any help much appreciated.

    T
     
    Amsterdam, Jan 31, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    echo $reg and see if it contains the expected content. Also note that the string comparison is case-sensitive.

    If you still need help, can you post your HTML form?
     
    nico_swd, Jan 31, 2008 IP
    Amsterdam likes this.
  3. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Do a

    
    print_r($_POST);
    
    PHP:
    ... near the top of your code to check to see if your "region" value is what you pressed on the form.

    Jay
     
    jayshah, Jan 31, 2008 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, it may be a silly question, but are you sure the form method is POST and not GET?
     
    TwistMyArm, Jan 31, 2008 IP
  5. Amsterdam

    Amsterdam Well-Known Member

    Messages:
    361
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #5
    Thanks for the suggestion. I tried it and discovered that the WYSYWIG view on my CMS was corrupting my HTML and the radio button values weren't being set. Once I switched to code view in the CMS to update the form page it worked perfectly!

    An important lesson learned. Thanks for your help.

    T
     
    Amsterdam, Jan 31, 2008 IP
  6. zerostar07

    zerostar07 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    just a side note (and longtime question of mine). Why is everyone using _POST or _GET and not _REQUEST, that contains both?:confused:
     
    zerostar07, Feb 2, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Because $_REQUEST is more insecure in some cases. It's a bad practice using it without a good reason.
     
    nico_swd, Feb 2, 2008 IP
  8. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Values in the $_POST array are limited to the options you give the user in a form. So, for example, a drop down list or radio button list has a small array of acceptable values.

    $_GET on the other hand can contain anything a savvy user puts into the url when he accesses the page.

    So in most cases, it's wise to use the $_POST array to help cut down on the user's ability to mess around with the input. This in turn helps make your site slightly more secure.

    - Walkere
     
    walkere, Feb 2, 2008 IP
  9. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #9
    A switch statement will perform faster in these kind of statements.
     
    Kaizoku, Feb 2, 2008 IP