Christopher
Christopher

Reputation: 2103

PHP IF/Else Not working

I have what should be a very simple question. I am using the following simple PHP if/else statement. When nomobile is not defined or not equal to "true", then the javascript shown below should execute. If it is equal to true, then it should not execute. However, the javascript is executing even when nombobile=true. There is probably a minor typo that is messing everything up, but I am driving myself crazy trying to find what is wrong and can't figure it out.

<?php
  if ($_GET["nomobile"]!="true")
    {echo '
    <script type="text/javascript">
    if (screen.width <= 699) {
    document.location = "http://www.heliohost.org/m/";
    }
    </script>';}
  else {} ?>

Upvotes: 1

Views: 487

Answers (4)

H2O
H2O

Reputation: 39

Remove Double quotes.

<?php
    if ($_GET["nomobile"]!=true) {
        echo '<script type="text/javascript"> if (screen.width <= 699) {  document.location = "http://www.heliohost.org/m/"; } </script>';
    }
    else {}
?>

Upvotes: -2

WWW
WWW

Reputation: 9860

I think the problem is with your Javascript. Verify that screen.width evaluates to a number that is less than or equal to 699 by alerting it (or logging it or however you choose to do it).

To clarify, when I try to browse to http://www.heliohost.org/?nomobile=true, I see the Javascript in your code. If I try to browse to http://www.heliohost.org/ or http://www.heliohost.org/?nomobile=foo, the Javscript isn't there. Which tells me that your Javascript isn't doing what you expect it to, not that there's a problem with your PHP code.

Upvotes: 1

Prasanth
Prasanth

Reputation: 5258

if(!isset($_GET['nomobile']) || (isset($_GET['nomobile']) && $_GET['nomobile'] != "true")){
    echo '<script type="text/javascript">
    if (screen.width <= 699) {
        document.location = "http://www.heliohost.org/m/";
    }
    </script>';
}

Upvotes: 0

jmbertucci
jmbertucci

Reputation: 8224

Could it be that you're not checking if the $_GET value is set? Taking this statement of yours and converting it into code should produce this:

When nomobile is not defined or not equal to "true"

if (!isset($_GET["nomobile"]) || $_GET["nomobile"] != "true"){ ... }

There might be issues happening if it's not set.

I hope that helps!

Upvotes: 2

Related Questions