简体   繁体   English

如何在应用程序的php页面之间传递值

[英]how to pass values between php pages of an app

I have a very simple app, it's only one php page (page A). 我有一个非常简单的应用程序,它只有一个php页面(第A页)。 I would like add one more php page (page B) that receives data from an html form of "page A". 我想再添加一个从“页面A”的html格式接收数据的php页面(第B页)。 I haven't found any tutorials about it. 我还没有找到任何关于它的教程。 Can you help me? 你能帮助我吗?

GET METHOD 获取方法

Page A: (eg. index.html ) 页面A :(例如index.html

<form action="welcome.php" method="get">
  Name: <input type="text" name="fname" />
  Age: <input type="text" name="age" />
  <input type="submit" />
</form>

Page B ( welcome php ) 第B页( welcome php

Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

When to use method="get"? 何时使用method =“get”?

When using method="get" in HTML forms, all variable names and values are displayed in the URL. 在HTML表单中使用method =“get”时,所有变量名称和值都显示在URL中。

Note: This method should not be used when sending passwords or other sensitive information! 注意:发送密码或其他敏感信息时不应使用此方法!

However, because the variables are displayed in the URL, it is possible to bookmark the page. 但是,由于变量显示在URL中,因此可以为页面添加书签。 This can be useful in some cases. 这在某些情况下很有用。

Note: The get method is not suitable for very large variable values. 注意:get方法不适用于非常大的变量值。 It should not be used with values exceeding 2000 characters. 它不应该使用超过2000个字符的值。

POST METHOD POST方法

Page A: (eg. index.html ) 页面A :(例如index.html

<form action="welcome.php" method="post">
  Name: <input type="text" name="fname" />
  Age: <input type="text" name="age" />
  <input type="submit" />
</form>

Page B ( welcome php ) 第B页( welcome php

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

When to use method="post"? 何时使用method =“post”?

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. 使用POST方法从表单发送的信息对其他人不可见,并且对要发送的信息量没有限制。

However, because the variables are not displayed in the URL, it is not possible to bookmark the page. 但是,由于变量未显示在URL中,因此无法为页面添加书签。

The PHP $_REQUEST Function PHP $ _REQUEST函数

The PHP built-in $_REQUEST function contains the contents of both $_GET, $_POST, and $_COOKIE. PHP内置的$ _REQUEST函数包含$ _GET,$ _POST和$ _COOKIE的内容。

The $_REQUEST function can be used to collect form data sent with both the GET and POST methods. $ _REQUEST函数可用于收集使用GET和POST方法发送的表单数据。

Example

Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.

If you are using Form on Page A then you can post the values from that form to another page B. 如果您在页面A上使用表单,则可以将该表单中的值发布到另一页面B.

if you are not using post or get you can still pass values from one page to another by creating session. 如果您没有使用帖子或获取,您仍然可以通过创建会话将值从一个页面传递到另一个页面。

Html form can have action set to Page B and $_POST or $_GET can give you data from page A to Page B Html表单可以将操作设置为页面B,$ _POST或$ _GET可以为您提供从页面A到页面B的数据

You are best off looking into a few tutorials this is PHP 101. 你最好看一些教程,这是PHP 101。

You can store the data in a session or cookie . 您可以将数据存储在会话cookie中 You can use a form to POST data to the next page. 您可以使用表单将数据发布到下一页。 you can use GET to send data in the uri 您可以使用GET在uri中发送数据

Here is a tutorial for POST and GET 这是POST和GET的教程

Edit : It sounds like you wish to pass the variables with a redirect. 编辑:听起来你希望通过重定向传递变量。 You could set the variables in session and retrieve them later or pass the variables in the redirect and fetch them using $_GET. 您可以在会话中设置变量并在以后检索它们,或者在重定向中传递变量并使用$ _GET获取它们。 Rather than redirecting to example.php redirect to example.php?var=value. 而不是重定向到example.php重定向到example.php?var = value。

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

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