CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Need Help with if than statements (http://www.codingforums.com/showthread.php?t=275651)

CodyJava 10-08-2012 02:46 AM

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.

Fou-Lu 10-08-2012 03:46 AM

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.";



patryk 10-08-2012 03:51 AM

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 :D


All times are GMT +1. The time now is 03:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.