Getting info from radio buttons.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Getting info from radio buttons.

Post by Mightywayne »

I have no idea how to do it. =o Normally you just set a variable to $_POST, but I'm not sure how this posts, and I have 5 options I don't know how to select, either.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

What does the form look like? Generally, you name the radio buttons all with the same "name" attr, and then the one that's selected has its "value" transmitted over the wire.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

Code: Select all

<p>
  <label>
  <input type="radio" name="RadioGroup" value="1" />
    first</label>
  <br />
  <label>
  <input type="radio" name="RadioGroup" value="2" />
    second</label>
  <br />
</p>
then you check the value

Code: Select all

if(isset($_POST['RadioGroup'])){
  $radio = $_POST['RadioGroup'];
   if($radio == "1") { 
      //.. do something
   }elseif($radio == "2"){
     //..do something else
   }
}
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Ah, gotcha. Thanks.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Ugh! Again! It can't just WORK.

Code: Select all

<form action="confmonster.php">
	 <center>
<image width="12%" height="12%" src="monsters/evilEgg.png"><input type="radio" name="choice" value="1"> Evil

<image width="12%" height="12%" src="monsters/goodEgg.png"><input type="radio" name="choice" value="2"> Good
<br>
<image width="12%" height="12%" src="monsters/cuteEgg.png"><input type="radio" name="choice" value="3"> Cute

<image width="12%" height="12%" src="monsters/normalEgg.png"><input type="radio" name="choice" value="4"> Worker
<br>
<image width="12%" height="12%" src="monsters/workEgg.png"><input type="radio" name="choice" value="5"> Normal<br><br>
<input type="submit" name="choice" value="I choose this one!"></center>
</form>
That's what my form looks like (ignore the images and stuff :p) but when I send it on over to confmonster.php, it has no value. I think it's the submit button. The confmonster.php looks like:

Code: Select all

$chosenmonster = $_POST["choice"];
	  
	 if (!isset($chosenmonster));
		die("Chosen monster... is not set.");
	  
	 if (empty($chosenmonster))
	 die("Erm, you should probably pick the egg you'd like first. ");

	  
	  // CHOOSING THE MONSTER. <333
	  // 1 = EVIL     2 = GOOD         3 = CUTE       4 = WORKER        5 = NORMAL
	  
	  if ($chosenmonster = 1)
	  die("Yup, it works.");
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

add this to the form

Code: Select all

method="post"
then

Code: Select all

//$chosenmonster = $_POST["choice"]; 
           
         if (!isset($_POST["choice"])){  die("Chosen monster... is not set."); }
           
        // if (empty($chosenmonster)) 
        // die("Erm, you should probably pick the egg you'd like first. "); 

           
          // CHOOSING THE MONSTER. <333 
          // 1 = EVIL     2 = GOOD         3 = CUTE       4 = WORKER        5 = NORMAL 
           
          if (isset($_POST["choice"])){
            if($_POST["choice"] == "1"){
              die("Yup, it works.");
            }
            //......... more code
         }
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Spectacular again! I hope you frequent these forums more often, you're a good helper.

Edit: Also, in future helps, I'd just like to say to not name your "submit" button *exactly* what you named your radio buttons. Just name it "submit" or something.
Post Reply