Octavius
Octavius

Reputation: 583

$_GET is not working

Creating a rating system and the info is not being transmitted through my $_GET variable. The code is below

    if (isset($_GET['item'], $_GET['rating'])){
echo 'Works!';
}

The variable is being entered in this code below

  <?php echo number_format( 
        $article['rating'],1); ?>
        <div class = "rate">
            Rate:
            <?php
            for ($x =1; $x<= $maximum_rating; $x++){
            ?>
                <a href="prestige.php?item=<?php echo $article['id']; ?>&rating=<?php echo $x;?>">
                <?php echo $x; ?></a>
            <?php
            }
            ?>

I am fairly new to programming so any ideas or tips would be greatly appreciated.

Upvotes: 4

Views: 346

Answers (2)

LSerni
LSerni

Reputation: 57418

The code you posted works. So the snag must be in the code you did not post:

  1. maybe the prestige.php page has a PHP error that prevents it from displaying anything; start with an empty file containing just <?php echo 'OK so far'; ?>.
  2. maybe the page contains code (security checks, frameworks...) that kills $_GET. (reduce the page to a minimum working case, without include/requires)
  3. maybe the page does work, but the output gets snarked by an untimely ob_end_clean() that was meant to "clean the page" before the real output started; (reduce the page to a minimum working case)
  4. maybe the page works, the string 'Works' is there, but you can't see it due to HTML markup, CSS, or other rendering problems (check the page source)
  5. the URL might be broken because the item code contains invalid URL characters (check what appears in the browser address bar)
  6. there might be an URL rewrite scheme that interferes (check .htaccess and the server logs)
  7. I just remembered something like this happening with international characters in the URL. Try with an ASCII-clean item code to see what happens.
  8. Just to be sure: verify there is no auto_prepend'ed file which might interfere.

Then, it might also be more than one of the above acting together. Often when debugging one unintentionally breaks some code, and even after fixing the first bug, the code doesn't start working again - this doesn't mean the fix was invalid.

I'm sorry -- I'm at the end of my options. I really look forward to knowing what the reason was. (Usually the more explanations I amass, the more the real answer tends to be "none of the above". When it happens to me, sometimes I wonder whether to start to believe in gremlins :-( ).

Upvotes: 2

Markus Hedlund
Markus Hedlund

Reputation: 24334

There are a couple of things you should do.

1. Instead of

prestige.php?item=<?php echo $article['id']; ?>&rating=<?php echo $x;?>

Use

prestige.php?<?= http_build_query(array('item' => $article['id'], 'rating' => $x), '&amp;') ?>

This will escape the parameters. Vars $article['id'] and $x could contain characters that break the HTML or URL.

2. Look at the Net tab in your Firebug/Chrome dev toolbar. Are there any redirects? What headers are sent?

Also look at the address bar to see if prestige.php really is loaded with the GET parameters.

3. Use a debug tool like XDebug to step through your code. You might have some code that resets the $_GET vars. Personally I use the IDE PHPed, but it's kinda expensive.

Upvotes: 4

Related Questions