Home > php教程 > php手册 > body text

[转] PHP在不同页面之间传值的三种常见模式

WBOY
Release: 2016-06-06 19:42:54
Original
958 people have browsed it

[转] PHP在不同页面之间传值的三种常见方式 转自: http://my.oschina.net/jiec/blog/196153 一. POST传值 post传值是用于html的form表单跳转的方法,很方便使用。例如: html form action='' method='' input type='text' name='name1' input type='hidden'

[转] PHP在不同页面之间传值的三种常见方式

转自: http://my.oschina.net/jiec/blog/196153     

 

一. POST传值

      post传值是用于html的

表单跳转的方法,很方便使用。例如:

<html>
 <form action='' method=''>
 <input type='text' name='name1'>
 <input type='hidden' name='name2' value='value'>
 <input type='submit' value='提交'>
 </form>
 </html>
Copy after login

  

&【本文来自鸿网互联 (http://www.68idc.cn)】nbsp; form中的action填入的是跳转页面的url路径,method填入post方法。form表单中的提交按钮按下后,就会把form中有name的内容都传到填入的url中,可以通过$_POST['name']获取,例如:

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

  

这里有个很方便的小技巧,在input标签中把type选为'hidden'时,这个input标签会隐藏起来,不在页面显示,但这input标签在form中,并且有name值和value值,同样会跟随提交按钮传递过去,这种隐藏标签可以传递一些不想显示出来的内容。

二.GET传值

GET传值是通过跟随url传递的,在页面跳转时,跟着url跳转。常用于标签的使用。例如:

<a href='delete.php?id=value'>点我跳转</a>
Copy after login

  

跳转进入xxx.php后,就能通过$_GET['id']获取传递的值。GET方法常用于URL的目的是删除或读取某个id的php文件。

三.SESSION传值

SESSION是全局变量的一种,经常用于用户登陆后保存用户id之类的常用数据。一旦保存到SESSION中,其他页面都可以通过SESSION获取,SESSION的使用要开启session:

<?php
//session赋值
   session_start();
   $_SESSION['one']=value1;
   $_SESSION['two']=value2;
 
//session值的读取:
   $one = $_SESSION['one'];
    
   //session值的销毁
   unset($_SESSION['one']);
?>
Copy after login

  

   以上三种方式是常用方式,大家可以参考使用。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!