0

I have tried to figure this out myself I am new to PHP and I know its easy but I am missing something. I am trying to create something that if someone click a Button have it pass a value called id from the button hyper link for example:

     <a href="abc123.html?id=d">Click Here...</a>

Then have the page abc123.html load and at the top of the page run this script below which is to see if id = f then if it does run the javascript code if not don't run it.

    <?php
    $vid = $_GET["id"];
    if ($vid != "f") {

       echo "<script type=""text/javascript"">"
            echo "if (screen.width<800)"
            echo "{"
            echo "window.location=""../mobile/default.html"""
            echo "}"
       echo "</script>"
    }
    ?>

However I am getting an PHP Undefined variable error from the Get ID. I have tried to use isset and that doesn't seem to work. But I know the error is caused if the value of id is blank because the id value might not always be available only if the user clicks on the abc123.html link. Can someone please tell me what I am doing wrong?

I have researched answers here:

Undefined variable php

Undefined variable error in PHP

and here...

http://matthom.com/archive/2005/02/19/php-passing-variables-across-pages

and here...

http://www.w3schools.com/php/php_if_else.asp

3
  • What does print_r($_GET); give you? Commented Jan 8, 2013 at 19:41
  • Gives me an error Array ( [id] => gd ) Commented Jan 8, 2013 at 20:09
  • Basiclly I am just trying to pass a value over a url such as ?id=f and then on the next page pull the id value and do an if then statement. But it's not working. This is what I have so far: <?php if (isset($_GET['id'])) $vid = $_GET['id']; if (isset($_GET['id']) != "f") { echo isset($_GET['id']); echo "<script type=\"text/javascript\">"; echo "if (screen.width<800)"; echo "{"; echo "window.location=\"../mobile/default.html\""; echo "}"; echo "</script>"; }; ?> Any suggestions as to what I am doing wrong? Thanks! Commented Jan 8, 2013 at 20:11

1 Answer 1

4

You must escape quotes and finish the statements with a semicolon:

echo "<script type=\"text/javascript\">";
Sign up to request clarification or add additional context in comments.

4 Comments

Or use single quotes: echo "<script type='text/javascript'>";
Here is what I have now is this right? <?php $vid = $_GET["id"]; if ($vid != "f") { echo "<script type=\"text/javascript\">"; echo "if (screen.width<800)"; echo "{"; echo "window.location=\"../mobile/default.html\""; echo "}"; echo "</script>"; }; ?> I am getting an error Undefined index on this part $vid = $_GET["id"];
@FrankG. Because $_GET["id"] is not defined. Use: if (isset($_GET['id'])) $vid = $_GET['id'];
@Peter As I mentioned above I am Basically I am just trying to pass a value over a url such as ?id=f and then on the next page pull the id value and do an if then statement. But it's not working. This is what I have so far: <?php if (isset($_GET['id'])) $vid = $_GET['id']; if (isset($_GET['id']) != "f") { echo isset($_GET['id']); echo "<script type=\"text/javascript\">"; echo "if (screen.width<800)"; echo "{"; echo "window.location=\"../mobile/default.html\""; echo "}"; echo "</script>"; }; ?> Any suggestions as to what I am doing wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.