Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - using if conditions to redirect to different pages

I want to redirect to different pages on an if condition using php.(not javascript) I tried as the example here. But unfortunately it doesn't works for me. :/ Here's my code:

<html>
    <body>
        <?php
            $time_type = $_POST['time_type'];
            $driver_type = $_POST['driver_type'];

            if (strcmp($time_type, "Arrival") && strcmp($driver_type, "Customer") )
            {
                header('location: http://localhost:8080/Project/Enter_Details.html');
                exit();
            }
        ?>
    </body>
</html>

This php is linked to a html page below.

<html>
    <title> Shopping Complex</title>

    <body>
        <form name = "Type" action = "Type.php" method = "POST">
            <fieldset>
                <table>
                    <p>
                        <tr>
                            <td><input type = "radio"
                                   name = "time_type" value = "Arrival">Arrival</input>
                                <input type = "radio"
                                   name = "time_type" value = "Departure">Departure</input>
                            </td>
                        </tr>
                        <tr>
                            <td><input type = "radio"
                                   name = "driver_type" value = "Staff">Staff</input>
                                <input type = "radio"
                                   name = "driver_type" value = "Customer">Customer</input>
                            </td>
                        </tr>
                    </p>
                    <p>
                        <tr>
                            <td><input type = "submit"
                                       value = "Go" id = "buttonId"></input>
                            </td>
                        </tr>
                    </p>
                </table>
            </fieldset>
        </from>
    </body>
</html>

Please Help me out of this......

like image 812
ThisaruG Avatar asked Jan 10 '23 19:01

ThisaruG


1 Answers

You're sending data (HTML tags) before sending the headers (PHP tags)

In order for it to work, make the PHP code the first thing in the document, not any HTML. So change this (also fixed above mentioned $driver_type and missing semicolon mistakes below):

<html>
    <body>
        <?php
            $time_type = $_POST['time_type'];
            $driver_type = $_POST['driver_type'];

            if (strcmp($time_type, "Arrival") && strcmp($driver_type, "Customer") )
            {
                header('location: http://localhost:8080/Project/Enter_Details.html');
                die;
            }
        ?>
    </body>
</html>

To just this:

<?php
    $time_type = $_POST['time_type'];
    $driver_type = $_POST['driver_type'];

    if (strcmp($time_type, "Arrival") && strcmp($driver_type, "Customer") )
    {
        header('location: http://localhost:8080/Project/Enter_Details.html');
        die;
    }
?>
like image 132
Steven Linn Avatar answered Jan 22 '23 20:01

Steven Linn