0

I'm very new to programming. I'm trying to get variables out of a selection box, the two text input boxes work ok. When I submit the form the red cat will always appear even if other options were selected.

        <form name="cat" action="" method="post">
        Name of Cat: <input class="inputbox" type="text" name="name" value="" /><br />
        Color: <select name="color"> 
            <option value="red">Red</option>
            <option value="blue">Blue</option>
            <option value="yellow">Yellow</option>
            <option value="black">Black</option>
            </select> <br>
        Weight: <input class="inputbox" type="number" name="weight" value="" /><br />
            <input type="submit" class="submit" name="submit" value="Submit" />
    </form>
<br>
<br>
<?php 
    if ($_POST['submit']) {
            $name = $_POST['name'];
            $color = $_POST['color'];
            $weight = $_POST['weight'];

        if ($color = 'red') {
            echo "<h2>" . $name . "</h2>";
            echo "<img src='http://www.clker.com/cliparts/9/7/D/t/L/K/red-cat-md.png'></a><br>" . $weight . " (kg)<br>";
        } else if ($color = 'blue') {
            echo "<h2>" . $name . "</h2>";
            echo "<img src='http://www.clker.com/cliparts/L/z/H/T/d/d/blue-cat-md.png'></a><br>" . $weight . " (kg)<br>";   
        } else if ($color = 'yellow') {
            echo "<h2>" . $name . "</h2>";
            echo "<img src='http://www.clker.com/cliparts/O/Y/K/K/u/r/yellow-cat-hi.png'></a><br>" . $weight . " (kg)<br>"; 
        } else if ($color = 'black') {
            echo "<h2>" . $name . "</h2>";
            echo "<img     src='http://content.mycutegraphics.com/graphics/halloween/cute-halloween-black-cat.png'></a><br>" . $weight . " (kg)<br>";   
        } else {
            //do nothing
        }
    }   
CameronB
  • 23
  • 2

3 Answers3

1

Error in your condition: if ($color = 'red'), try my code below

   if ($_POST['submit']) {
        $name = $_POST['name'];
        $color = $_POST['color'];
        $weight = $_POST['weight'];

    if ($color == 'red') {
        echo "<h2>" . $name . "</h2>";
        echo "<img src='http://www.clker.com/cliparts/9/7/D/t/L/K/red-cat-md.png'></a><br>" . $weight . " (kg)<br>";
    } else if ($color == 'blue') {
        echo "<h2>" . $name . "</h2>";
        echo "<img src='http://www.clker.com/cliparts/L/z/H/T/d/d/blue-cat-md.png'></a><br>" . $weight . " (kg)<br>";   
    } else if ($color == 'yellow') {
        echo "<h2>" . $name . "</h2>";
        echo "<img src='http://www.clker.com/cliparts/O/Y/K/K/u/r/yellow-cat-hi.png'></a><br>" . $weight . " (kg)<br>"; 
    } else if ($color == 'black') {
        echo "<h2>" . $name . "</h2>";
        echo "<img     src='http://content.mycutegraphics.com/graphics/halloween/cute-halloween-black-cat.png'></a><br>" . $weight . " (kg)<br>";   
    } else {
        //do nothing
    }
Thi Tran
  • 681
  • 5
  • 11
1

You are using a single equals sign which is used for assigning a variable:

$variable = 'foo';

You need double equals signs when performing a conditional statement:

if ($variable == 'foo') {
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
0

Please try to add this code to your so that if you have selected some option and submit then the page will show what you have selected on load

<select name="color" value="<?php echo $_POST['color']; ?>"> 
Eason.Luo
  • 134
  • 7