Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a data to PHP page when clicking on a Link?

Tags:

php

I have several links in an HTML page (say some 100 - 120). What I exactly need is to send different data to another PHP page (say display.php) when I click on each link. I tried a pathetic method,

<form action="display.php" method="post"> // or GET
   <input type="hidden" name ="data" value="Data1" />
   <input type="submit" value="Link 1" />
</form>`  
.  
.  
.  

<form action="display.php" method="post">
   <input type="hidden" name ="data" value="Data120" />
   <input type="submit" value="Link 120" />
</form>

Is there any simple way to reduce the usage of these 120 forms?

like image 925
Sarvap Praharanayuthan Avatar asked Mar 14 '12 05:03

Sarvap Praharanayuthan


3 Answers

You can put data directly on the href of your tag.
Ex:

<a href="./display.php?data=Data1&data2=Data120">Click here</a>

Then in your display.php file :

<?php
    if(isset($_GET["data"]) && isset($_GET["data2"]))
    {
        $data = $_GET["data"];
        $data2 = $_GET["data2"];
    }
?>

Here you go.

like image 179
Yoann Avatar answered Oct 18 '22 00:10

Yoann


yes sure try to use tags with id's

<a href="display?data=data1">link1</a>

hope this helps

like image 1
Muhannad A.Alhariri Avatar answered Oct 18 '22 00:10

Muhannad A.Alhariri


Try this

<form action='display.php' method='post'>
  <input type="submit" value="Link 1" name='1' onclick='setHidden(this)'/>
  <input type="submit" value="Link 2" name='2' onclick='setHidden(this)'/>
  <input type="submit" value="Link 3" name='3' onclick='setHidden(this)'/>
  <input type="submit" value="Link 4" name='4' onclick='setHidden(this)'/>

  <input type="hidden" id='data' name ="data" value="" />
</form>

// Javascript

<script type='text/javascript'>
  function setHidden( key)
  {
       var dataStr='Data';
       dataStr+=key.name;

        document.getElementById('data').value=dataStr;  
  }
</script>

then in display.php file

<?php
   if(isset($_POST['data']))
   {
      $data=$_POST['data'];

      echo $data;
   }  
?>
like image 1
Naveen Kumar Avatar answered Oct 18 '22 00:10

Naveen Kumar