Page 1 of 1

simple php problem (probably)

Posted: Sat Feb 27, 2010 3:13 pm
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

Re: simple php problem (probably)

Posted: Sat Feb 27, 2010 3:34 pm
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.";
    }                                
}
?>

Re: simple php problem (probably)

Posted: Sun Feb 28, 2010 8:01 am
by geoffmuskett
Thanks for your help Darhazer!