Kamikaza Kamikaza - 4 months ago 11
CSS Question

PHP form redirect if get elseif else

I have a web form html where the customer select the report they want to print. Now I must get the value from the select menu and redirect the customer to the correct report page

example

<form action="print-report.php" name="print" method="get" target="_blank"> <select name="period">
<option value="daily" selected>Daily Report</option>
<option value="anual">Anual Report</option>
<option value="weekly">Weekly Report</option>
<option value="monthly">Montly Report</option>
</select>
<input type="submit" value="Print Report"></form>


Now the other page I must get the type of report the customer need and redirect like this but I do not know how...

<?php
if get daily redirect to daily.php
elseif get weekly redirect to weekly.php
<?php


How to do this ?

Answer

When you submit data using PHP through a form using the POST method, the form data will be passed via the $_POST array which is accessible by your PHP file. Since yours uses the GET method, the form data is passed via the $_GET array. The key for both arrays will be the name of your form element.

Hence, to obtain the value of the selected drop-down list, you can have something like this in your PHP code:

<?php
$SelectedValue = $_GET['period'];
if($SelectedValue == "daily")        { header("Location: daily.php"); }
else if($SelectedValue == "weekly")  { header("Location: weekly.php"); }
// add your other options and redirects here
?>

Though it's worth noting that you might be interested in using a switch-case statement too:

<?php
switch($_GET['period'])
{
    case "daily":  header("Location: daily.php");  break;
    case "weekly": header("Location: weekly.php"); break;
    // add your other options and redirects here
    default:       header("Location: error.php");  break;
}
?>
Comments