form script help please (Solved)


Hi again,

I have another newbie problem. I’m trying to create a simple script that processes a form. When submitted it takes users to a certain page.

The form uses radio buttons and when submit is clicked it ‘should’ take you to your selected page. What it’s doing is taking me to the same page for each option.

Here is the code I’m using:

foreach ($_POST as $field => $value) if ($field = "addcategory") { header("location: ChooseHorseCat.php"); } elseif ($field = "addhorse") { header("location: AddHorse.php"); } elseif ($field = "deletehorse") { header("location: DeleteHorse.php"); } elseif ($field = "mainpage") { header("location: HorseCatalog.php"); exit(); }

ChooseHorseCat.php is the page I’m taken to for each option.

I’ve checked all the name cases in the form etc.
Can someone see what I’m doing wrong? Can I not use ‘header’ this way? Does the first use override using it again?

Also, when I tried making the last elseif, just else, I got an error saying “unexpected ‘{’ on line …etc” I’m not sure why.

Does anyone have any suggestions as to what’s wrong or perhaps another way to do it?

I could use just hyperlinks I know, but I like the idea of using buttons to do it. Plus, lol, I’m stubborn I guess. :)


Well with radio buttons, only one value will be returned. With something like:



When the form is submitted, the $_POST[“color”] variable will contain whichever option was selected. If the first option was selected, $_POST[“color”] would be “red”, and “green” if the second option was selected, and so on. From there it’s a simple case of something like this:


if($_POST[“color”] == “red”){
// do something
elseif($_POST[“color”] == “green”){
// do something else
elseif($_POST[“color”] == “blue”){
// do something else



Thx Da Warriah,

I had actually commented out the foreach part at one stage, but I don’t think I had the syntax quite right I ‘think’ I used $_POST[“value”] in the if statement. Which didn’t work… :roll:

I really appreciate your help :smiley:


Minor addendum: rather then using a buch of elseif’s you might want ot check out switchs. They are cleaner, easier to read and run faster.



ohh, :smiley:

Thx lig, it’s nice to know that i WAS trying something that could have worked :)


I’m not sure exactly what value I used for the variable originally but I got it working with switch :smiley:

If anyone’s interested, this is what the code looks like now:


	switch ($_POST["options"])
		case "addcategory":
			header("location: ChooseHorseCat.php");
		case "addhorse":
			header("location: ChooseHorseCat.php");
		case "deletehorse":
			header("location: DeleteHorse.php");
		case "mainpage":
			header("location: horsecatalog.php");
			header("location: AdminFront.php");