开发者

Either my if or my variables are messed up

I recently just started playing around in PHP and got myself a small project/homework.

The idea is very simple - by getting a number by user input, draw a circle.

I got the circle working with imagearc, everything scales beautiful by the number the user type in.

However, there seems to be a color problem. On my main page, I created four radio buttons with 4 different colors. The radio button choose what color the circle has.

I can't get it work though. The circle is not displaying anymore, even though I checked everything. Here's the code I try to use:

<form method="post" action="circle.php">
<input type="radio" name="color" value="blue" class="color">Blue<br />
<input type="radio" name="color" value="red" class="color">Red<br />
<input type="radio" name="color" value="green" class="color">Green<br />
<input type="radio" name="color" value="yellow" class="color">Yellow<br />
<input type="submit" value="Send" name="submit" />
</form>

That's from index.php, here's what I try to do

$color = $_POST['color'];

if ($color == 'red') {
    $superColor = $red;
    }

// make some color
$white  = imagecolorallocate($img, 255, 255, 255);
$red    = imagecolorallocate($img, 255, 0, 0);

开发者_运维技巧// draw the circle
imagearc($img, ($height + 10) / 2, ($width + 10) / 2, $circumference, $circumference, 0, 360, $superColor);

I'm still new, and still learning, so I guess there's an easier and prettier way of doing it. This is just my try. What am I doing wrong here?

Thanks in advance

EDIT: Thanks for the quick answers! I have tried what you guys (or girls) told me to do, change from ID to name and put the == 'red' in quotes - it doesn't work though.

I think it has something to do with my if statement. Shouldn't it be possible to give the information stored in $red to $superColor by doing a simple $superColor = $red?

If not, what should I do instead? Nothing shows up in my imagearc, if I change $superColor to $red, it shows up a nice, red circle.

An echo test shows that $_POST['color']; works just fine


You index $_POST with names, not ids. Change your input definitions to name="color".


You need a name attribute on your radio buttons.


if ($color == 'red') {

"red" is a string. You need to quote it.


try if ($color == "red") {. You forgot to put quotes around red.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜