Page 1 of 1

Getting info from radio buttons.

Posted: Sat Feb 10, 2007 5:20 pm
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.

Posted: Sat Feb 10, 2007 5:36 pm
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.

Posted: Sat Feb 10, 2007 5:37 pm
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
   }
}

Posted: Sat Feb 10, 2007 9:52 pm
by Mightywayne
Ah, gotcha. Thanks.

Posted: Sun Feb 11, 2007 9:27 am
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.");

Posted: Sun Feb 11, 2007 9:36 am
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
         }

Posted: Sun Feb 11, 2007 9:46 am
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.