What am I doing wrong $_GET

60 views Asked by At

I am trying to get a value from a URL and it's not working. Here is the code:

<?php
if (isset($_GET['pag'])) {
    $sel_continut = $_GET['pag'];
} else {
    $sel_continut = "no value sent";
}

echo "<a href=\"continut.php?pag=" . $informatie_tabel['nume_meniu'] . "\">" . $denumire_pagina . "</a> <br />";

Can someone help me understand what I'm doing wrong?

1

There are 1 answers

3
David On

In response to your comments...

This is the URL being used to load the page:

http://localhost/extragere_din_baza_de_date.php

Note that there is no query string value. So this code:

$_GET['pag']

isn't going to find anything. The value was never set. So your code is always going to enter the else condition. You later create a link with a query string value:

http://localhost/continut.php?pag=Home

But that's for a completely different page which hasn't even been requested yet. If you reference $_GET['pag'] on the continut.php page's code, then you'll see the value.

What I'm trying to say is... You can't read a query string value before it's been sent. The good news is that you don't need to read this value. Since you're in the same code which sets the value on the link, then clearly you already have this value. Right here:

$informatie_tabel['nume_meniu']