简体   繁体   English

如何访问传递到下一页的参数

[英]How to access parameters that were passed to next page

As part of my requirement i constructed the href link with a variable associated with it as follows 作为我的要求的一部分,我构造了带有链接的href链接,如下所示:

<h5><a href="http://working.artefacts.co.in/single.php?',$ID,'"> ', $NAME, '</a></h5>

Upon click, page is being redirected correctly to single.php and even $ID value can be seen on the browser. 单击后,页面将正确重定向到single.php,甚至在浏览器中也可以看到$ ID值。

But I dont know how to extract ID parameter in single.php 但是我不知道如何在single.php中提取ID参数

Can any body please tell me how can i access $ID value in single.php page 任何机构都可以告诉我如何在single.php页面中访问$ ID值

<h5><a href="http://working.artefacts.co.in/single.php?id='.$ID.'"> ', $NAME, '</a></h5>

<?php echo $_REQUEST['id']; ?>

you can use something like sessions, cookies or GET / POST variables. 您可以使用会话,cookie或GET / POST变量之类的东西。 Sessions and cookies are quite easy to use, with session being by far more secure than cookies. 会话和cookie非常易于使用,会话比cookie更加安全。 More secure, but not completely secure. 更安全,但不完全安全。

Session: 会议:

//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];

Remember to run the session_start() statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser. 请记住,在尝试访问$ _SESSION数组之前,以及在将任何输出发送到浏览器之前,都要在这两个页面上运行session_start()语句。

Cookie: 曲奇饼:

//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];

GET and POST GET和POST

You can either add the variable in the link to the next page: 您可以在指向下一页的链接中添加变量:

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>

This will create a GET variable, or include a hidden field in a form that submits to page two: 这将创建一个GET变量,或在提交到第二页的表单中包含一个隐藏字段:

<form method="get" action="page2.php">
    <input type="hidden" name="varname" value="var_value">
    <input type="submit">
</form>

And then on page two 然后在第二页

//Using GET
$var_value = $_GET['varname'];

//Using POST
$var_value = $_POST['varname'];

//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];

Just change the method for the form to post if you want to do it via post. 如果您想通过邮寄方式,只需更改邮寄方式。 Both are equally insecure, although GET is easier to hack. 两者同样不安全,尽管GET更易于破解。

', $NAME, ' ',$ NAME,'

PHP $_GET can also be used to collect form data after submitting an HTML form with method="get". 在使用method =“ get”提交HTML表单后,PHP $ _GET也可以用于收集表单数据。

$_GET can also collect data sent in the URL. $ _GET还可以收集在URL中发送的数据。

Assume we have an HTML page that contains a hyperlink with parameters: 假设我们有一个HTML页面,其中包含带有参数的超链接:

 <a href="test_get.php?subject=PHP&web=W3schools.com">Test $GET</a> 

When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can then access their values in "test_get.php" with $_GET. 当用户单击链接“ Test $ GET”时,参数“ subject”和“ web”将发送到“ test_get.php”,然后您可以使用$ _GET在“ test_get.php”中访问它们的值。

Supposing the $ID is set to 2, your URL is: 假设$ID设置为2,则您的URL为:

http://working.artefacts.co.in/single.php?2

So, in your single.php page you have to write: 因此,在您的single.php页面中,您必须编写:

$id = $_SERVER['QUERY_STRING'];

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

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