Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Need Help with if than statements

    Hi I'm trying to echo out some text when the user submits a form. My problem is i can't get an if statement to work for the option tag under the select tag. I played around with the value and name of the options with no luck.

    Here is my code, all my other if statements work except this.

    if($SelectedOption == $_Post['tube']){ //selct option output
    echo "for your interest in Tube Men.<br>";
    }else if($selectedOption == $_Post['oldtube']){
    echo "for your interest in 70's Tube Men.<br>";
    }else if($selectedOption == $_Post['dinosaur']){
    echo "for your interest in Tube Dinosaur.<br>";
    }else if($selectedOption == $_Post['gorilla']){
    echo "for your interest in Tube Gorilla.<br>";
    }else if($selectedOption == $_Post['spider']){
    echo "for your interest in Tube Spiderman.<br>";
    }else{
    echo "for your interest in our products.<br>";
    }


    Here is what im linking them to.

    <select name="product">
    <option value="tube" name="1">Tube Man</option>
    <option value="oldtube" name="2">70's Tube Man</option>
    <option value="dinosaur" name="3">Tube Dinosaurs</option>
    <option value="gorilla" name="4">Tube Gorilla</option>
    <option value="spider" name="5">Tube Spiderman</option>
    </select>

    Thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Unlike functions, classes, namespaces, and insensitive created defined constants, PHP variables are ALWAYS case sensitive. There is no $_Post, the correct variable is $_POST.

    Pull a switch on $_POST['product']. Option's I don't believe can be named if I recall my RFC's, so those would be ignored. The value(s) chosen will be under the $_POST['product'], so it would work well with a switch (PHP allows switches on strings since they are considered primitive data):
    PHP Code:
    switch ($_POST['product'])
    {
        case 
    'tube':
            echo 
    "for your interest in Tube Men.<br>";
            break;
        case 
    'oldtube':
            echo 
    "for your interest in 70's Tube Men.<br>";    
            break;
        case 
    'dinosaur':
            echo 
    "for your interest in Tube Dinosaur.<br>";
            break;
        case 
    'gorilla':
            echo 
    "for your interest in Tube Gorilla.<br>";
            break;
        case 
    'spider':
            echo 
    "for your interest in Tube Spiderman.<br>";
            break;
        default:
            echo 
    "for your interest in our products.<br>";

    You could also use an array for even simpler lookups:
    PHP Code:
    $aOptions = array(
        
    'spider' => 'for your interest in Tube Spiderman.',
        
    // and so forth
    );

    if (isset(
    $aOptions[$_POST['product']]))
    {
        print 
    $aOptions[$_POST['product']] . PHP_EOL;
    }
    else
    {
        print
    "for your interest in our products.";


  • Users who have thanked Fou-Lu for this post:

    CodyJava (10-08-2012)

  • #3
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    how about using switch?
    PHP Code:
    switch ($_POST['product']) {
        case 
    'tube':
            echo 
    'thank you for ineterst in tube men';
            break;
        case 
    'dinosau':
            echo 
    'thank you for ineterst in tube dinosaur';
            break;
        case 
    'whatever':
             echo 
    'thank you for whatever';
        default:
           echo 
    'sellect something next time';

    and btw
    Code:
    <select name="some_name">
         <option value="option1">something</option>
    </select>
    sends $_POST['some_name'] = 'option1' not $_POST['option1']
    and there's no 'name' property in <option>

    ----edit-----
    damn i was hoping to be first
    Last edited by patryk; 10-08-2012 at 04:54 AM.

  • Users who have thanked patryk for this post:

    CodyJava (10-08-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •