0

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

3
  • You cannot ask for the code directly here.You'll haveto show us what is the code that you've tried and then ask it here for more info on how to ask question see help center. Commented Apr 5, 2017 at 3:34
  • @IshanMahajan ok tks imma update my code here Commented Apr 5, 2017 at 3:37
  • it always store the last bcz session variable doesn't change at all u r always doing $_SESSION["hashtag_search"]=$hashtag[$i]; so at last in for loop session always store the last $hashtag[$i] Commented Apr 5, 2017 at 5:04

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

i tried your method but got a blank page ( i got a issue with my error_log not appearing )
oh got it it's the problem with the hashtag itself ( the hashtag block the url ). Thank you !
you should also change your for loop to foreach($hashtag as $value). Documentation here link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.