简体   繁体   中英

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM