单击html链接时获取价值 [英] Get value when an html link is clicked

查看:76
本文介绍了单击html链接时获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击链接时是否可以在锚标记中获取文本的值?例如 如果您有<a href="#">Tables</a> 是否可以在会话变量之类的变量中检索"Tables"值并在您的页面中使用它? 我有几个类别的项目(属于链接),我希望能够获得单击的任何类别的值,并使用它从我的数据库中提取该类别中列出的产品. 谢谢.

Is it possible to get the value of the text in an anchor tag when the link is clicked? e.g if you have <a href="#">Tables</a> is it possible to retrieve the value "Tables" in a variable like a session variable and use it across your pages? I have several categories of items(which are links) and i want to be able to get the value of any category clicked and use it pull products listed in that category from my database. Thanks.

这是我尝试过的 <a href="#" onclick= "<?php $_SESSION[value] = "Table" ?>">Table</a> 但是没有得到价值. 抱歉,应该早点发布

This what i tried <a href="#" onclick= "<?php $_SESSION[value] = "Table" ?>">Table</a> But didn't get the value. Apologies, should have posted it earlier

推荐答案

发布后,您的代码未提交,因此从未处理您的PHP:

As posted, your code does not submit so your PHP is never processed:

<a href="#"

当您将href值设为#"时,不会提交新请求.但是,您可以修改锚点,以将类别提交回PHP脚本,如下所示:

When you have "#" as the href value, no new request is submitted. However, you can modify your anchor to submit the category back to your PHP script like this:

<a href="yourscript.php?category=Table">Table</a>

然后在yourscript.php中,您可以使用$ _GET读取值:

Then in yourscript.php you can read the value using $_GET:

$selected_category = $_GET['category']; // Get 'Table'

要在会话中设置值,请首先确保已在脚本顶部附近启动会话:

To set the value in your session, first be sure that you have started your session near the top of your script:

session_start();

然后按如下所示保存值:

then save the value like this:

$_SESSION['selected_category'] = $_GET['category'];

这篇关于单击html链接时获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆