Skip to content Skip to sidebar Skip to footer

PHP Header Location Not Working Properly

Can someone point me out why the below isn't working? It only redirects to the first location even if I choose different radio buttons. PHP: if (isset($_POST['submit'])) { if

Solution 1:

You have to use the comparison operator == instead of =

if (isset($_POST['submit'])) {

    if (!empty($_POST['electronics'])) {

        if ($_POST['electronics'] == "camera") {
            header("location: camera.php");
        }
        else if ($_POST['electronics'] == "cell") {
            header("location: cellphones.php");
        }
        else if ($_POST['electronics'] == "cable") {
            header("location: cables.php");
        }
        else if ($_POST['electronics'] == "tv") {
            header("location: tv.php");
        }
    }

...

Also, the exit() is also redundant as you are already redirecting to another page.


Solution 2:

= is assignment. == is equality. You've confused the two.


Solution 3:

To add to the other answers, when you use the assignment operator (=) instead of the comparison operators (== or ===), the assignment is passed from right to left.

So the following is true:

"camera" == $_POST['electronics'] = "camera"

Which in your case is true enough to satisfy the if

It's the same behaviour that lets you make multiple assignments with one value.

eg:

$foo = $bar = 10;

$foo and $bar are both assigned 10.


Post a Comment for "PHP Header Location Not Working Properly"