简体   繁体   English

通过HTML按钮存储PHP变量

[英]Store PHP variable through HTML button

I am trying to store a PHP session variable through a HTML button. 我正在尝试通过HTML按钮存储PHP会话变量。 For example I run a FOR loop contains a HTML button and it shows up 5 buttons: A ,B ,C, D, E . 例如,我运行一个包含HTML按钮的FOR loop ,它显示了5个按钮: 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 我想单击按钮C ,它将重定向到显示(回显)$ 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 在search-hashtag.php上,我得到了

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 但是SESSION变量将存储最后的$hashtag[$i] (当$ i达到最大值时),而不存储单击按钮的$hashtag[$i]

You are defining the session in each loop so it won't work. 您正在每个循环中定义会话,因此它将无法正常工作。 You should use a GET Method to send the variable. 您应该使用GET方法发送变量。

index.php index.php

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

search-hashtag.php search-hashtag.php

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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