John Doe
John Doe

Reputation: 3

PHP radio button not posting correctly

I'm stuck in a bit of a pickle I can't figure out. Any help is appreciated.

I set up an HTML radio table

<div class="radio-group">
	<label class="heading">Choose your game</label><br/>

<table>
	<tr>
		<td>
		<input type="radio" name="radio" value="Radio 1"> Radio 1
		</td>
	</tr>
	<tr>
		<td>
		<input type="radio" name="radio" value="Radio 2"> Radio 2
		</td>
	</tr>
	<tr>
		<td>
		<input type="radio" name="radio" value="Radio 3"> Radio 3
		</td>
	</tr>
	<tr>	
		<td>
		<input type="radio" name="radio" value="Radio 4"> Radio 4
		</td>
	</tr>
</table>
</div>
	 	</br>
		<input type="submit" name="submit" value="Submit" />
		<input type="reset" name="reset" value="Reset"/>
		  
	</form>
</div>
      

My PHP page will POST the correct selection, but loads other stuff it shouldn't. How do I differentiate between the radio selections if there are all the same name? If I name it separately, you can click on every selection.

if (isset($_POST['submit'])) {

if(isset($_POST['radio']))
{echo '<iframe etc etc></iframe>;
}
else{echo '<iframe other></iframe>;}
}

Upvotes: 0

Views: 2538

Answers (1)

Kaboom
Kaboom

Reputation: 684

You can check what value was posted and then make your echo statement based on that. Here's an example of two radio buttons in the same group and name

<input type="radio" name="radio" value="1"> Radio 1
<input type="radio" name="radio" value="2"> Radio 2

and here is the PHP to see which one has been submitted

if(isset($_POST['radio'])) {
    $value = $_POST['radio'];
    if($value == "1") {
        echo "SOMETHING HERE FOR VALUE 1";
    } elseif($value == "2") {
        echo "SOMETHING HERE FOR VALUE 2";
    }
}

Notice how the value is set to something simple like an integer, it makes it easier to check it later on and then display the appropriate or desired outcome for that selected button :)

Upvotes: 1

Related Questions