Rab Rab - 2 months ago 7
HTML Question

PHP how to redirect radio if an answer is checked?

Hello I'm Pretty New To PHP I'm trying to Make an Script if the user selects an Radio Option It Will Redirect them to an site.

PHP:

if (isset($_POST["badge"]) ) {
if($_POST["badge"]==="Blue"){

header("Location: /form-error.shtm");
}


HTML:

<form action="badge.php" method="post">
<p>
<input class="with-gap" name="group1" type="radio" id="red" />
<label for="red">Red</label>
</p>
<p>
<input class="with-gap" name="group1" type="radio" id="blue" />
<label for="blue">Blue</label>

<div class="col offset-s7 s5">
<button class="btn waves-effect waves-light red darken-1" type="submit">Submit
<i class="mdi-content-send right white-text"></i>
</form>

Answer

You are missing values for the radio button and then alone you can check whether the value posted is equal to the value or not.

You HTML need to be altered like this

<form action="badge.php" method="post">
<p>
<input class="with-gap" value="Red" name="group1" type="radio" id="red" />
<label for="red">Red</label>
</p>
<p>
<input class="with-gap" value="Blue" name="group1" type="radio" id="blue"  />
<label for="blue">Blue</label>

<div class="col offset-s7 s5">
<button class="btn waves-effect waves-light red darken-1" name="save" type="submit">Submit
<i class="mdi-content-send right white-text"></i>

Since i have provided a name for the submit button i can check with that in PHP page.

badge.php

<?php
if(isset($_POST['save']))
{
if (isset($_POST["badge"]) ) {
if($_POST["badge"]=="Blue"){
header("Location: /form-error.shtm");
}
}
}
?>
Comments