'PHP - If clause Statement to open different pages when conditions are met [closed]

I am a php novice and I have a form with an input filed where users will enter their phone number and it will open their respective page for them.

I want to write a php script with if clause statement such that if a condition is met, it will open the respective page based on the phone number entered.

For example, I want an if clause statements such that

if 1 is entered, page1 shld be opened
or
if 2 is entered, page2 should be opened
or
if 3 is entered, page3 should be opened
or
if 4 is entered, page4 should be opened
and many pages as possible such that in the end
if none of the conditions are met, a default page should be opened.

How do I go about this?



Solution 1:[1]

In your form submit page you can do a redirect to the appropriate page.

Ex:

<form action="submit.php" method="post">
    <input type="number" name="phone" />
</form>

submit.php

<?php
$phone_no = $_POST['phone'];
header("Location: detail.php?number=".$phone_no); // or any page you want..
die();
?>

If you want it to be conditions based, then you can do:

<?php
$phone_no = $_POST['phone'];
$redirect_location = "default.php";
if($phone_no == "11111"){
    $redirect_location = "11111.php"
}else if($phone_no == "22222"){{
    $redirect_location = "22222.php"
}

header("Location:".$redirect_location); 
die();
?>

Please consider upvoting and accepting the answer if it helped you.

Solution 2:[2]

You can also try the switch case. The switch case is fast as compare to if as well and look nice and cleaner because all conditions in separate case.

<?php
$phone_no = $_POST['phone'];
switch($phone_no){
    case "123456789":
         header("Location: www.site.org");
         break;
    case "987654321":
        header("Location: www.site2.org");
        break;
    default:
        header("Location: www.site3.org");
        break;
}
?>

Solution 3:[3]

As per my experience in PHP you can achieve this by writing a PHP script similar to as shown below:

<?php

$id = $_POST['phone_number']; 
/* Assuming Phone_number is the field name in the form if not change it to the same name as given in your HTML form */

if($id == '12345678')
header('Location of the page where you want to redirect');
else if ($id == '12345687')
header('Location of the second page where you want to redirect');
else
header(default.php');

?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NewProgrammer
Solution 2
Solution 3 Udit Agarwal