Home  >  Q&A  >  body text

Title Rewrite: When extracting text from a URL, the results returned using $_GET are undefined

I'm trying to use $_GET to extract an id from get-albums-genres.php?gid=".$id." but it returns me undefined!

After extracting the data from the genre table, I ran a SQL query using an array to get the music genre data

$dbcursor = $db->query("
    SELECT *
    FROM GENRES_TBL
    ORDER BY ID
    ");

$genres = [];
while($takegenre = $dbcursor->fetch_object()) $genres[] = $takegenre;

After this, I created an html table containing <a tags, using a foreach loop to create hyperlinks for each type of music.

<?php
    foreach($genres as $gs)
    {
        $id = $gs->ID;
        echo "
            <a class='gentons' href='get-albums-genres.php?gid=".$id."'>".$gs->GENRE."</a>";
    }
    ?>
</table>

With these hyperlinks, I want to display a table of all albums of the selected genre. To achieve this, I plan to extract the id from the href and use it in a SQL query to concatenate the album type id to the type id, but when extracting the id with $_GET, I am getting the # of the album. When declaring a variable in ##php file and using $_GET, it returns me undefined value.

$genre_id = $_GET['gid'];

echo"$genre_id";   //undefined

if(isset ($_GET['gid'])){
    echo"yes";
}else{
    echo"no";
}  //yes

The Javascript code with events is as follows:

$(document).on('click', 'a.gentons', function(e)
            {
            $.get('get-albums-genres.php?gid=' + $('input[name="gid"]').val(),
            function(data)
                {
                $('#albums-list').html(data);
                });
            return false;
            })

This is weird because I did the exact same thing in another php file, I entered the artist's

name or best songs and all his albums were found and displayed, and There I use the $_GET method without any problems. I also don't understand why when I hover the cursor over a hyperlink it shows the link and the corresponding id, but when I try to extract it I have a problem.

Enter image description

P粉895187266P粉895187266431 days ago578

reply all(1)I'll reply

  • P粉811349112

    P粉8113491122023-09-08 11:42:55

    You have a few questions here. First, instead of using the href in the link tag (you definitely don't want anyone to actually visit the URL, right?), let's set the href to # and put our type id in a data in properties. Let's make single quotes and double quotes clear while we're at it.

    echo '<a class="gentons" data-gid="' . $id . '" href="#">' . $gs->GENRE . '</a>';
    

    Now in our JS, we can cancel the event using preventDefault(), get the type ID, and leave the rest unchanged.

    $(document).on('click', 'a.gentons', function (e)
    {
        e.preventDefault();
        
        $.get('get-albums-genres.php?gid=' + $(this).data('gid'),
            function (data)
            {
                $('#albums-list').html(data);
            });
        
        return false;
    });
    

    This should work, or at least help you resolve the next error.

    reply
    0
  • Cancelreply