-1

I am trying to get the value of an anchor tag using PHP code. When I click the hyperlink, however, I am not getting the value.

My code is as follows:

<?php
    $gettingevents = "SELECT event_name FROM events";
    $resultgettingevents = mysqli_query($con,$gettingevents) or die(mysqli_error($con));

    while($getevents = mysqli_fetch_assoc($resultgettingevents)){
        $gettablenames = $getevents['event_name'];
        echo '<a id="events" href="registeredevents.php?tablename='.$getevents['event_name'].'">'.$gettablenames.'</a>';

        if(isset($_GET['tablename'])){
            $clickedtable =$_GET['tablename'];
            echo "the clicked table is ".$clickedtable;
        }else{
            echo "";
        }

    }
?>

I am getting the following error:

undefined index: tablename in

What is wrong with my code? How can I get the hyperlink value?

John C
  • 8,223
  • 2
  • 36
  • 47
Madhi
  • 45
  • 1
  • 12
  • why you used `$clickedtable =$_GET['tablename']` this line at the end of while loop? – Md. Abutaleb Dec 22 '16 at 19:04
  • ermmm to get the hyperlink value... – Madhi Dec 22 '16 at 19:06
  • 2
    you also need to remove the space in `registeredevents.php?tablename =` - besides putting the GET array first and checking if it's set/not empty – Funk Forty Niner Dec 22 '16 at 19:07
  • 1
    you should wrap `$clickedtable = $_GET['tablename'];` in `if(isset[$_GET['tablename']) { }`, and I suppose it should live outside the while loop, right? – Jeff Dec 22 '16 at 19:07
  • @Jeff i tried your method.i am not getting the values – Madhi Dec 22 '16 at 19:12
  • @Fred-ii- sorry.. i am not quite understand your point...say me once again in detail..i removed white space in the link.. – Madhi Dec 22 '16 at 19:13
  • `registeredevents.php?tablename =` the space in there counts as a character, it needs to read as `registeredevents.php?tablename=` along with what I said to use a conditional statement. – Funk Forty Niner Dec 22 '16 at 19:14
  • plus, that's also open to an SQL injection – Funk Forty Niner Dec 22 '16 at 19:15
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – mister martin Dec 22 '16 at 19:17
  • okay i got your point...i changed my code..but still it doesn't get the names.. – Madhi Dec 22 '16 at 19:18

1 Answers1

0

Try

<?php
    $gettingevents = "SELECT event_name FROM events";
    $resultgettingevents = mysqli_query($con,$gettingevents) or die(mysqli_error($con));
    while($getevents = mysqli_fetch_assoc($resultgettingevents)){
        $gettablenames = $getevents['event_name'];
        echo '<a id="events" href="registeredevents.php?tablename='.$getevents['event_name'].'">'.$gettablenames.'</a>';
        if(isset($_GET['tablename'])){    
          $clickedtable =$_GET['tablename'];
        }
    }
    ?>
Abhishek
  • 1,008
  • 1
  • 16
  • 39
  • the `if ` will be executed when you actually visit on of those generated urls,for the first time you are visiting the `url = registeredevents.php`.But when you click on the url whcih is genarated then your url will look like `url = registeredevents.php?tablename=xyz`. Then the `$clickedtable = xyz` – Abhishek Dec 22 '16 at 19:17
  • i clicked the link and i echoed **$clickedtable** it is not printing..it's not entering into if loop..... – Madhi Dec 22 '16 at 19:26
  • can you add the url as an example which comes after clicking on generated url – Abhishek Dec 22 '16 at 19:31
  • please check my code just now i edited my post...my url link is `''.$gettablenames.''` – Madhi Dec 22 '16 at 19:34
  • please share URL from address bar after clicking on one of those generated URL. The code seems correct we have to debug from the URL from address bar – Abhishek Dec 22 '16 at 19:36
  • [link](http://localhost/Examples/Surya/registeredevents.php?tablename%20=Activity%20Based%20Events) – Madhi Dec 22 '16 at 19:39
  • Yes copy that url and paste here i want to have a look – Abhishek Dec 22 '16 at 19:43
  • http://localhost/Examples/Surya/registeredevents.php?tablename%20=Activity%20Based%20Events – Madhi Dec 22 '16 at 19:44
  • You still have that space between `tablename` and `=`! Thats the problem. – Jeff Dec 22 '16 at 19:50
  • Or please for god sake copy your code only and paste. It will work – Abhishek Dec 22 '16 at 19:59