Store PHP variable through HTML button

I am trying to store a PHP session variable through a HTML button. For example I run a FOR loop contains a HTML button and it shows up 5 buttons: A ,B ,C, D, E. I want to click on the button C and it will redirect to a new page that shows up (echo) $C

How could I achieve that?

I have tried the following code:

for($i=0;$i<count($hashtag);$i++){ 

 echo '<button type="submit" name="insert" value="';    

 $_SESSION["hashtag_search"]=$hashtag[$i]; 

 echo '"><span class="label label-danger" ><a target="_blank" href="http://link.net/search-hashtag.php">'.$hashtag[$i].' ('.$hashtag_count[$i].')</a></span></button>';

}

on the search-hashtag.php i got

echo $_SESSION["hashtag_search"];

but the SESSION variable would store the last $hashtag[$i] (when $i maxed) not the $hashtag[$i] of the clicked button

Upvotes: 0

Views: 856

Answers (1)

SunriseM
SunriseM

Reputation: 995

You are defining the session in each loop so it won't work. You should use a GET Method to send the variable.

index.php

<a target="_blank" href="http://link.net/search-hashtag.php?hashtag='.$hashtag[$i].'">

search-hashtag.php

$a = $_GET["hashtag"];
echo($a);

Upvotes: 1

Related Questions