我有4页第1页有一个带有输入的表单第2页没有表单,但重定向第3页有一个带有输入的表格第4页有一个带有输入的表单。
编辑-(添加代码)
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<?php require_once 'detect.php'; ?>
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<script type="text/JavaScript">
<!--
setTimeout("location.href = 'page4';",5000);
-->
</script>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
我正在考虑使用session(),因为这将是处理此问题的最方便方法,收集所有页面的输入并将其发布到最终的php处理程序,在本例中为"verNote.php"。
如果我能找到一个切实可行的解决方案,我也将不胜感激。
谢谢。
- 试试这个,这里不需要middle.php
page1.php
<?php
session_start();
if ( isset( $_POST[ "anything" ] ) ){
$_SESSION[ "anything" ] = $_POST[ "anything" ]; // by this value from the form are stored in $_SESSION[ "anything" ]
header( "Location: page2.php" );
}
else header( "Location: page1.php" );
?>
<html>
<head>
<title>Session</title>
</head>
<body>
Welcome to page 1.
<br/>
<br/>
<form method="post" action="page1.php">
Type anything
<input type="text" name="anything" />
<input type="submit" value="Send value to page 2" />
</form>
</body>
</html>
page2.php
<?php
session_start();
?>
<html>
<head>
<title>Session</title>
</head>
<body>
Welcome to page 2.
<br/>
<br/>
This is the value from page 1 :
<input type="text" value="<?php echo $_SESSION['anything'];?>" />
</body>
</html>
在每个php页面中写入第一行
session_start(); // this must be the first line of every page in php
然后,创建会话变量如下:
$_SESSION['sex']=<variable_name_to_store> ;
稍后,在要访问此会话变量值的每个页面中,直接使用$_session['sex']。类似<?php echo $_POST['sex'] ?>
使用Session,您不需要在每个页面的每种形式中存储相同的值(就像您对"sex"所做的那样)。使用Session,您只需存储一次值,它就会保持不变。现在举个例子。
对于将数据发送到第2页的第1页,需要一个中间脚本从输入表单中获取值并将其存储在Session中。下一个代码就是一个例子。
page1.php(将值发送到第2页)。
<?php
session_start();
?>
<html>
<head>
<title>Session</title>
</head>
<body>
Welcome to page 1.
<br/>
<br/>
<form method="post" action="middle.php">
Type anything
<input type="text" name="anything" />
<input type="submit" value="Send value to page 2" />
</form>
</body>
</html>
middle.php(从第1页获取值并将其存储在会话中)
<?php
session_start();
if ( IsSet( $_POST[ "anything" ] ) )
{ $_SESSION[ "anything" ] = $_POST[ "anything" ];
header( "Location: page2.php" );
}
else header( "Location: page1.php" );
?>
page2.php(显示从第1页收到的值)
<?php
session_start();
?>
<html>
<head>
<title>Session</title>
</head>
<body>
Welcome to page 2.
<br/>
<br/>
This is the value from page 1 :
<input type="text" value="<?php echo $_SESSION["anything"];?>" />
</body>
</html>
Ruse Yee,创建3个文本文件,并将其命名为page1.php、middle.php和page2.php,复制粘贴相应的代码,然后从浏览器运行page1.php:
http://localhost/page1.php
如有必要,您可以添加端口:
http://localhost:80/page1.php
如果你有更多的页面和发送更多数据的表单,那么你将需要更多的middle.php文件。
希望这对你有帮助。