Example code for three methods of passing values between php pages

  • 2020-05-12 02:34:12
  • OfStack

It is common to see value transfer between different pages in project development. In web's work, this article gives you a list of three common ways to do this.

1. POST spread value

The post pass is for html < form > Form jump method, very convenient to use. Such as:


<html>
<form action='' method=''>
<input type='text' name='name1'>
<input type='hidden' name='name2' value='value'>
<input type='submit' value=' submit '>
</form>
</html>

action in form fills in the url path of the jump page, and method fills in the post method. When the submit button in the form form is pressed, all contents with name in form will be sent to url, which can be obtained through $_POST['name']. For example:


<?php
$a=$_POST['name1'];
$b=$_POST['name2'];
?>

Here's a handy tip. When you select type as 'hidden' in the input TAB, the input TAB will be hidden and not displayed on the page, but the input TAB is in form and has an name value and an value value, which will also be passed along with the submit button. This hidden TAB can pass something that you don't want to show.

2. GET spread value

GET passes the value by following url. When the page jumps, jump with url. Commonly used in < a > Use of labels. Such as:

< a href='delete.php?id=value' > Let me jump < /a >

After jumping into xxx.php, you can get the passed value through $_GET['id']. The GET method is often used with the purpose of URL to delete or read the php file of an id.

3. SESSION spread value

SESSION is one of the global variables that are often used to save common data such as id after a user logs in. Once it is saved into SESSION, other pages can be obtained through SESSION, and session should be opened for the use of SESSION:


<?php

//session The assignment 

 session_start();

 $_SESSION['one']=value1;

 $_SESSION['two']=value2;

//session The value of the read :

 $one = $_SESSION['one'];

 //session The value of the destroyed 

 unset($_SESSION['one']);

?>

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: