Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2012, 02:46 AM   PM User | #1
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
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.
CodyJava is offline   Reply With Quote
Old 10-08-2012, 03:46 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.";

Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
CodyJava (10-08-2012)
Old 10-08-2012, 03:51 AM   PM User | #3
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
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 03:54 AM..
patryk is offline   Reply With Quote
Users who have thanked patryk for this post:
CodyJava (10-08-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:49 AM.


Advertisement
Log in to turn off these ads.