简体   繁体   English

当我单击按钮并将其存储在会话变量中时,如何获取链接

[英]How can i get link when i click on button and stored in session variable

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. 我有foreach循环,当循环输出数组中保存的项目时,效果很好,但是我想将每个EACH项目存储在会话变量中,所以我可以在没有页面刷新的情况下在其他地方使用此信息,我正在尝试创建会话在foreach循环内的变量,但是当然,每次循环通过不同的项时变量都需要具有不同的数据。假设在下面的代码中,我具有三个不同的产品链接,当我单击按钮时,如何获得该特定链接特定产品存储在会话变量中,而无需重新加载页面。当我从一个页面重定向到另一页面时,我使用会话概念使用了会话变量中的链接。

session.php 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 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 我的查询是当我从一页重定向到另一页时,并且当我回显会话变量(产品链接)时,它仅回显最后一个值,而不是使用特定的产品链接

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 那你应该打电话给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;
}
?>

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

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