$ _GET does not work

Creating a rating system, and the information is not passed through the variable $ _GET. Code below

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

The variable is 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 new to programming, so any ideas or advice would be greatly appreciated.

+4
source share
2 answers

There are several things you should do.

1. Instead

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

Using

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

This will avoid the parameters. Vars $article['id'] and $x may contain characters that violate HTML or URLs.

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

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

3. To run the code, use a debugging tool, such as XDebug . Perhaps you have some code that resets vars $_GET . Personally, I use the PHPed IDE, but it's quite expensive.

+4
source

The code you posted works. Thus, the snag should be in code that you did not publish:

  • perhaps the prestige.php page has a PHP error that prevents it from displaying anything; start with an empty file containing only <?php echo 'OK so far'; ?> <?php echo 'OK so far'; ?> .
  • Perhaps the page contains code (security checks, frameworks ...) that kills $ _GET. (reduce the page to the minimum working case, not including / not requiring).
  • perhaps the page is working, but the output is being blinked out of time by ob_end_clean() , which is designed to "clear the page" before the actual output starts; (reduce the page to the minimum working case)
  • The page may be working, the "Works" line is there, but you do not see it because of HTML markup, CSS, or other rendering problems (check the source of the page).
  • The URL may be corrupted because the element code contains invalid URLs (check what is displayed in the address bar of the browser)
  • there may be a URL rewrite scheme that interferes (check .htaccess and server logs)
  • I just remembered something similar with international characters in the url. Try the ASCII-clean code to see what happens.
  • To make sure: make sure there is no auto_prepend 'ed file that might interfere.

Then it can also be more than one of the above actions together. Often during debugging, one of them inadvertently interrupts some code, and even after fixing the first error, the code does not start again - this does not mean that the correction was invalid.

Sorry - I'm at the end of my options. I really hope to find out what is the reason. (Usually, the more explanations I find, the more the real answer tends to be β€œnone of the above.” When this happens to me, sometimes I wonder whether to start believing in gremlins :-().

+2
source

Source: https://habr.com/ru/post/1437887/


All Articles