simple php problem (probably)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
geoffmuskett
Forum Newbie
Posts: 22
Joined: Sat Feb 27, 2010 3:09 pm

simple php problem (probably)

Post by geoffmuskett »

Hi there, I'm new to PHP and this is just a practise script... not important but if anyone could point out where I'm going wrong I'd appreciate it. Basically the script doesn't seem to do anything... doesn't get any error messages either...

Code: Select all

 
<?php
if (isset($_GET['submit'])) {
    $sloth = $_GET['Sloth'];
    $llama = $_GET['Llama'];
    $monkey = $_GET['Monkey'];
    $giraffe = $_GET['Giraffe'];
    $gorilla = $_GET['Gorialla'];
    $giraffe = $_GET['Giraffe'];
    $whale = $_GET['Whale'];
    $moth = $_GET['Moth'];
    
    if (isset($_GET['sloth'])) {
        echo "Sloth's are boring";
    } elseif (isset($_GET['llama'])) {
        echo "Llama's are ok I guess";
    } elseif (isset($_GET['monkey'])) {
        echo "Yeah, monkeys are cool!";
    } elseif (isset($_GET['giraffe'])) {
        echo "Giraffe's are a bit wierd";
    } else {
        echo "Please choose don't choose Whale or Moth. No reason, just choose something else.";
    }                                  
}
?>
 

Code: Select all

 <form method=get action="<?php echo $_SERVER['PHP_SELF'] ?>">    <select>        <option name=sloth>Sloth</option>        <option name=llama>Llama</option>        <option name=monkey>Monkey</option>        <option name=giraffe>Giraffe</option>        <option name=gorilla>Gorilla</option>        <option name=whale>Whale</option>        <option name=moth>Moth</option>    </select>    <input type="submit" value="submit" /></form>
Thanks
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: simple php problem (probably)

Post by Darhazer »

Your HTML is wrong, that's why you don't submit anything:

Code: Select all

<form method=get action="<?php echo $_SERVER['PHP_SELF'] ?>">
    <select name="select">
        <option value=sloth>Sloth</option>
        <option value=llama>Llama</option>
        <option value=monkey>Monkey</option>
        <option value=giraffe>Giraffe</option>
        <option value=gorilla>Gorilla</option>
        <option value=whale>Whale</option>
        <option value=moth>Moth</option>
    </select>
    <input type="submit" name="submit"" value="submit" />
</form>
And your PHP in this case would be:

Code: Select all

<?php
if (isset($_GET['submit'])) {
    switch ($_GET['select']) {   
     case 'sloth':
        echo "Sloth's are boring";
        break;
    case 'llama':
        echo "Llama's are ok I guess";
    break;
    case 'monkey':
        echo "Yeah, monkeys are cool!";
    break;
    case 'giraffe':
        echo "Giraffe's are a bit wierd";
    break;
    default:
        echo "Please choose don't choose Whale or Moth. No reason, just choose something else.";
    }                                
}
?>
geoffmuskett
Forum Newbie
Posts: 22
Joined: Sat Feb 27, 2010 3:09 pm

Re: simple php problem (probably)

Post by geoffmuskett »

Thanks for your help Darhazer!
Post Reply