Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
What does print_r($_GET); give you? –  ioums Jan 8 '13 at 19:41
    
Gives me an error Array ( [id] => gd ) –  Frank G. Jan 8 '13 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! –  Frank G. Jan 8 '13 at 20:11
add comment

1 Answer

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

echo "<script type=\"text/javascript\">";
share|improve this answer
1  
Or use single quotes: echo "<script type='text/javascript'>"; –  Rocket Hazmat Jan 8 '13 at 18:59
    
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"]; –  Frank G. Jan 8 '13 at 19:05
    
Anyone know why I am getting this Undefined error for $vid = $_GET["id"]; ??? –  Frank G. Jan 8 '13 at 19:35
    
@FrankG. Because $_GET["id"] is not defined. Use: if (isset($_GET['id'])) $vid = $_GET['id']; –  Peter Jan 8 '13 at 19:41
    
@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? –  Frank G. Jan 8 '13 at 20:12
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.