简体   繁体   中英

PHP/JavaScript Redirect Issue

As a side project I have attempted to create a COVID-19 Self Assessment tool. Everything has worked up until now. When I post all of the form data into the PHP script, and use this method, but I keep getting an error that I cannot modify the headers.

    $name = $symtpoms = $home = $travel = $contact = $doctorOrdered = $appExposed = null;

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $name = input($_POST["name"]);
        $symtpoms = input($_POST["symptoms"]);
        $home = input($_POST["home"]);
        $travel = input($_POST["travel"]);
        $contact = input($_POST["contact"]);
        $doctorOrdered = input($_POST["doctorOrdered"]);
        $appExposed = input($_POST["appExposed"]);

        if (isset($symtpoms) && isset($home) && isset($travel) && isset($contact) && isset($doctorOrdered) && isset($appExposed)) {
            if ($symtpoms == true || $home == true || $travel == true || $contact == true || $doctorOrdered == true || $appExposed == true) {
                Header("Location: #safeFalse");
            } else {
                Header("Location: #safeTrue");
            }
        }
    }

    function input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

Then, when I try the code below, no matter what outcome I should arrive at, I always get sent to #safeFalse

    $name = $symtpoms = $home = $travel = $contact = $doctorOrdered = $appExposed = null;

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $name = input($_POST["name"]);
        $symtpoms = input($_POST["symptoms"]);
        $home = input($_POST["home"]);
        $travel = input($_POST["travel"]);
        $contact = input($_POST["contact"]);
        $doctorOrdered = input($_POST["doctorOrdered"]);
        $appExposed = input($_POST["appExposed"]);

        if (isset($symtpoms) && isset($home) && isset($travel) && isset($contact) && isset($doctorOrdered) && isset($appExposed)) {
            if ($symtpoms == true || $home == true || $travel == true || $contact == true || $doctorOrdered == true || $appExposed == true) {
                echo "<script type='text/JavaScript'>
                window.location.href = '#safeFalse';
                </script>";
            } else {
                echo "<script type='text/JavaScript'>
                window.location.href = '#safeTrue';
                </script>";
            }
        }
    }

    function input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

Greatly appreciate all help. Thanks

For the first problem:

This arises when you have output something prior to setting the header. Somewhere in your script (a part you haven't shown here), you are outputting something. Remove all the output and the error will disappear.

an example of this could be:

echo "output!";
header("Location: file.php"); //WE FAIL... WE OUTPUTTED SOMETHING IN THE LINE ABOVE

for the second problem:

Well, taking a look at the following statement:

 if (isset($symtpoms) && isset($home) && isset($travel) && isset($contact) && isset($doctorOrdered) && isset($appExposed))

it basically says: if all of these are set (set and not NULL), go on.

You then say:

if ($symtpoms == true || $home == true || $travel == true || $contact == true || $doctorOrdered == true || $appExposed == true)

Notice how you use 2 = signs? This means that it evaluates truthy and falsy. If ANY of these variables evaluate to true , which is the case, considering I highly doubt all of the variables are given falsy values, it'll ALWAYS go into the if statement.

You basically say: If a OR b OR c OR e etc. evaluates to true, it should proceed.

The only way this would fail would be if ALL the variables evaluate to a falsy value, which again, I highly doubt, because that would mean all the variables would have to be either 0 , null or "" , in which we can basically nullify the null value, since that has already been handled by the isset .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM