dongxian0421 2015-12-08 03:38
浏览 43
已采纳

当我点击按钮并存储在会话变量中时,如何获得链接

I have foreach loop which when looping outputs the items held in an array.This works great, but I want to store a few things per EACH item in session variables so I can use this information elsewhere without page refresh,I am trying to create session variables inside the foreach loop,but of course the variables need to have different data every time it loops through a different item.Suppose in below code i have three different product link,when i click on button how do i get that particular link for that particular product is stored in session variable without reloading a page.When i redirect from one page to other i used that link which is in session variable using session concept.

session.php

<?php
$a=array(
    array("Htc 526 black","htc.jpg",23000,"http://www.flipkart.com"),
    array("Iphone 6s","apple.jpg",43000,"http://www.amazon.com"),
    array("Sony xperia c3 dual","sony.jpg",19000,"http://www.snapdeal.com")
);

foreach($a as $value)
{
    session_start();
    $_SESSION['link']=$value[3];
?>
<script>
    function redirect()
    {
        window.location="redirection.php";
    }
</script>
<button onclick="redirect();">Click Me</button>

<?php
}
?>

redirection.php

<?php
if(session_id()=="")
{
    session_start();
}
$deep_link=$_SESSION['link'];
echo $deep_link;?>

My query is when i redirect from one page to other and when i echo the session variable(product link),it only echo's the last value Instead it takes that particular product links

  • 写回答

1条回答 默认 最新

  • douyan8961 2015-12-08 04:02
    关注

    your array assign has only one room ,so create increase number for multiple array like this. // two parameter making style

    <?php
    $i = 0; //init number for ++
    $a=array(
    array("Htc 526 black","htc.jpg",23000,"http://www.flipkart.com"),
    array("Iphone 6s","apple.jpg",43000,"http://www.amazon.com"),
    array("Sony xperia c3 dual","sony.jpg",19000,"http://www.snapdeal.com")
    );
    
    foreach($a as $value)
    {
    session_start();
    $_SESSION['link'][$i]=$value[3];    
    ?>
    <script>
    function redirect(i,phone)
    {
        window.location="redirection.php?d="+i+"&pno="+phone;   //can use one more parameter like this ...
    }
    </script>
    <button onclick="redirect(<?= $i ?>,'<?= $value[0]; ?>');">Click Me</button> //can add parameter by ',' as you want
     //eg link structure =><button onclick="redirect(1,'Htc 526 black');">Click Me</button>
    <?php
    $i++;  //increase array number
    }
    ?>
    

    then you should call in redirection.php

    <?php
    if(session_id()=="")
    {
    session_start();
    }
    $i = $_GET['d'];
    $p = $_GET['pno'];//call by parameter name
    $deep_link=$_SESSION['link'][$i];
    echo $deep_link."</br>".$p;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致