简体   繁体   中英

Problem on using $_SESSION inside HTML anchor href

I have a sample code here

<li><a href="<?php $_SESSION['category']= 'A'; echo 'sample.php'; ?>">A</a></li>
<li><a href="<?php $_SESSION['category']= 'B'; echo 'sample.php'; ?>">B</a></li>
<li><a href="<?php $_SESSION['category']= 'C'; echo 'sample.php'; ?>">C</a></li>
<li><a href="<?php $_SESSION['category']= 'D'; echo 'sample.php';?>">D</a></li>
<li><a href="<?php $_SESSION['category']= 'E'; echo 'sample.php';?>">E</a></li>
<li><a href="<?php $_SESSION['category']= 'F'; echo 'sample.php'; ?>">F</a></li>
<li><a href="<?php $_SESSION['category']= 'G'; echo 'sample.php';?>">G</a></li>
<li><a href="<?php $_SESSION['category']= 'H'; echo 'sample.php';?>">H</a></li>
<li><a href="<?php $_SESSION['category']= 'I'; echo 'sample.php';?>">I</a></li>
<li><a href="<?php $_SESSION['category']= 'J'; echo 'sample.php';?>">J</a></li>

as you can see i am trying to save into session the letter to be sent into sample.php. But even if i press 'B' what gets sent over to the sample.php is 'J' (the last letter) . tried clicking the other letters too its always 'J'.

can anybody help on what i am doing wrong?

Thank you very much

Try this

<a href="sample.php?value=A">A</a></li>
<a href="sample.php?value=B">A</a></li>
<a href="sample.php?value=C">A</a></li>

It's because your PHP code run and return the HTML output to your browser. Because of that, all of them are 'J'.

For solve the problem use code like this:

<li><a href=“sample.php?v=A”>A</a></li>
<li><a href=“sample.php?v=B”>B</a></li>
<li><a href=“sample.php?v=C”>C</a></li>

And you can get v value in your sample.php with this code:

<?php
$linkv=$_GET[“v”];

If ($linkv==“A”)
{
   //write your code here
}

If ($linkv==“B”)
{
   //write your code here
}

If ($linkv==“C”)
{
   //write your code here
}

?>

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