form script help please (Solved)


#1

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. :)


#2

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

[code]



[/code]

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:

[code]<?php

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

?>[/code]


#3

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:


#4

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.

Reference:
http://www.php.net/switch


#5

ohh, :smiley:

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


#6

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:

[php]

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

[/php]