11 Vote

HTML Form: Redirection depending on Radiobutton or Checkbox State

Tutorial by Progger99 | 2018-02-03 at 10:59

Today I want to show you how you can implement an automatic forwarding with PHP after submitting an HTML form. It should be redirected to a page that depends on the user input in the form. For this, I would like to present to you both, a solution with radio buttons as well as another solution with checkboxes.

Radiobutton-Redirection

We have three different radio buttons in our form. Each radio button stands for a different page. When you click on the Submit button you will be redirect to one of these pages, depending on which of the radio buttons was selected before.

<form method="post" action="rbredirector.php">
  <label><input type="radio" name="rb" value="1"> Page 1</label>
  <label><input type="radio" name="rb" value="2"> Page 2</label>
  <label><input type="radio" name="rb" value="3"> Page 3</label>
  <input type="submit" value="Submit">
</form>

In the script on the receiver page, we get the state of the selection via $_POST['rb']. Then we use a switch condition to realize a different forwarding for each case. The forwarding takes place via the PHP function header(), which accepts any URL after "Location:".

<?php

$rb = $_POST['rb'];

switch ($rb) {
   case 1:
      header("Location: http://www.example.com/page1.php");
      exit;
   break;

   case 2:
      header("Location: http://www.example.com/page2.php");
      exit;
   break;

   case 3:
      header("Location: http://www.example.com/page3.php");
      exit;
   break;
}

?>

It is important that no output of the script should happen before calling the header() function, otherwise the forwarding will not work. For example, neither an echo in the script may be called before, nor may any character appear before the initial "<?php".

Checkbox-Redirection

Next, let's look at a redirect solution depending on the state of some checkboxes. Instead of the three radio buttons, we have now 3 checkboxes built into our HTML form.

<form method="post" action="cbredirector.php">
  <label><input type="checkbox" name="cb1" value="1"> 1</label>
  <label><input type="checkbox" name="cb2" value="1"> 2</label>
  <label><input type="checkbox" name="cb3" value="1"> 3</label>
  <input type="submit" value="Submit">
</form>

You can only select one radio button at the same time, while you can check multiple checkboxes at once, so the checkboxes can be combined as desired. So, while we only had to consider three different cases for our radio buttons, now seven different combinations of checkboxes are possible we have to process in our script.

<?php

$cb1 = $_POST['cb1'];
$cb2 = $_POST['cb2'];
$cb3 = $_POST['cb3'];

if ($cb1 && $cb2 && $cb3) {   // all checkboxes are selected
   header("Location: http://www.example.com/page123.php");
   exit;
} else if ($cb1 && $cb2) {    // 1 and 2 selected
   header("Location: http://www.example.com/page12.php");
   exit;
} else if ($cb1 && $cb3) {    // 1 and 3 selected
   header("Location: http://www.example.com/page13.php");
   exit;
} else if ($cb2 && $cb3) {    // 2 and 3 selected
   header("Location: http://www.example.com/page23.php");
   exit;
} else if ($cb1) {            // only 1 selected
   header("Location: http://www.example.com/page1.php");
   exit;
} else if ($cb2) {            // only 2 selected
   header("Location: http://www.example.com/page2.php");
   exit;
} else if ($cb3) {            // only 3 selected
   header("Location: http://www.example.com/page3.php");
   exit;
}

?>

Depending on which of the checkboxes have been selected in which combination, we forward here to another page. Of course it would also be possible to summarize certain cases and not to offer a special page for every combination.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.