1

I got couple of li elements and using them as a links to welcome.php. In welcome.php I need to know which one was clicked.

     <ul>
     <li><a href='welcome.php'>sport</a></li>
     <li><a href='welcome.php'>relax</a></li>
     <li><a href='welcome.php'>gym</a></li>
     </ul>

I can't use jquery. Is there any other way to do this?

Christoffer
  • 7,470
  • 9
  • 39
  • 55
user7303261
  • 128
  • 12
  • Please check the link : http://stackoverflow.com/questions/9037737/text-of-the-clicked-link – Pragati Apr 20 '17 at 07:05
  • you can check here [http://stackoverflow.com/questions/5116929/get-clicked-li-from-ul-onclick](http://stackoverflow.com/questions/5116929/get-clicked-li-from-ul-onclick) you get answer – Dhaval Rajpara Apr 20 '17 at 07:08

4 Answers4

2

The simplest way to do this might be to append a query parameter to your links:

 <ul>
 <li><a href="welcome.php?category=sport">sport</a></li>
 <li><a href="welcome.php?category=relax">relax</a></li>
 <li><a href="welcome.php?category=gym">gym</a></li>
 </ul>
Norwæ
  • 1,575
  • 8
  • 18
1

you can incorporate the value in the links and get them with PHP like this

<a href="welcome.php?x=sport">

Where x is the variable you're going to get by using this in your php:

$x = $_GET["x"];

You'd need to do the same with all other links. but you only need to get the value once ofcourse

I was a little bit too late with posting this, but I think mine might have a better explaination so I'm keeping it

Martijn Vissers
  • 712
  • 5
  • 29
0

I have prepare one solution for you using simple html and Javascript :

<html>
<body>
<ul>
 <li><a href='welcome.php' id="sport">sport</a></li>
 <li><a href='welcome.php' id="relax">relax</a></li>
 <li><a href='welcome.php' id="gym">gym</a></li>
</ul>
</body>

<script>
    var el = document.getElementById('sport');
    el.onclick = showMsg1;

    var el = document.getElementById('relax');
    el.onclick = showMsg2;

    var el = document.getElementById('gym');
    el.onclick = showMsg3;

    function showMsg1() {
      //alert('Sport Clicked!');
      //Do what ever you want after click of "sport"
      window.location = "welcome.php?type=sport";
      return false;
    }
    function showMsg2() {
      //alert('relax Clicked!');
      //Do what ever you want after click of "relax"
      window.location = "welcome.php?type=relax";
      return false;
    }
    function showMsg3() {
      //alert('Gym Clicked!');
      //Do what ever you want after click of "Gym"
      window.location = "welcome.php?type=gym";
      return false;
    }
</script>

I am sure this will helpful to you. Thank you :)

Unknown_Coder
  • 764
  • 6
  • 24
0

You can use $_GET for post value of li I prepare an example for you

index.php

<form action="welcome.php" method="post">
    <ul>
        <li><a href="welcome.php?var=sport">sport</a></li>
        <li><a href="welcome.php?var=relax">relax</a></li>
        <li><a href="welcome.php?var=gym">gym</a></li>
    </ul>
 </form>

welcome.php

$var = $_GET["var"];
echo $var;