我正在测试一个SESSION,在此之前的前三页将打印到最后一页。但是我很难让它工作。一切顺利,直到最后一页。这是代码:
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
$_SESSION['age'] = $_POST ['age'];
echo $_SESSION['name'];
echo $_SESSION['email'];
echo $_SESSION['age'];
}
?>
我在会话变量中出现空白或索引错误。谁能以正确的方式打印用户输入的会话数据?
这些是第一页,第二页和第三页:
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_1.php">
<input type="text" name="name" placeholder="enter name"></input>
<input type="submit"> next</input>
</form>
<?php
//Set session variables
if (isset($_POST['submit'])) {
$_SESSION['name'] = $_POST ['name'];
}
?>
</body>
</html>
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_2.php">
<input type="text" name="email" placeholder="enter email"></input>
<input type="submit"> next</input>
</form>
<?php
// Set session variables
?>
</body>
</html>
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
$_SESSION['age'] = $_POST ['age'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="post_test.php">
<input type="text" name="age" placeholder="enter age"></input>
<input type="submit"> next</input>
</form>
<?php
// Set session variables
?>
</body>
</html>
答案 0 :(得分:0)
HTML中没有名称为submit
的输入元素,因此PHP代码的以下部分将始终为false
:
if (isset($_POST['submit'])) {
将HTML中的“提交”按钮更改为以下内容,发布表单后,上面的行将评估为true
:
<input name="submit" type="submit"> next</input>
答案 1 :(得分:0)
input
元素是self-closing
,因为您不需要</input>
-只需使用/>
要将Next
显示为提交按钮,您可以使用value属性-即:<input type='submit' value='Next' />
分配了会话变量后,您无需继续尝试设置它,并且如果该变量在POST数组中不可用,则会遇到错误-因此请在创建变量之前使用isset
进行测试>
<?php
session_start();
if ( isset( $_POST['submit'], $_POST['name'] ) ) {
$_SESSION['name'] = $_POST['name'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_1.php">
<input type="text" name="name" placeholder="enter name" />
<input type="submit" name='submit' value='Next'>
</form>
</body>
</html>
<?php
session_start();
if ( isset( $_POST['submit'], $_POST['email'] ) ) {
$_SESSION['email'] = $_POST['email'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_2.php">
<input type="text" name="email" placeholder="enter email" />
<input type="submit" name='submit' value='Next'>
</form>
</body>
</html>
<?php
session_start();
if( isset( $_POST['submit'],$_POST['age'] ) ) {
$_SESSION['age'] = $_POST['age'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="post_test.php">
<input type="text" name="age" placeholder="enter age" />
<input type="submit" name='submit' value='Next'>
</form>
</body>
</html>
为了进一步帮助您解决问题,我快速整理了一个页面演示,该演示基于表单提交来获取和设置会话变量。希望对您有帮助
<?php
session_start();
#https://stackoverflow.com/questions/54194496/what-am-i-doing-wrong-with-this-phone-session-im-trying-to-print-to-screen/54194890?noredirect=1#comment95217192_54194890
if( !empty( $_GET['reset'] ) ){
unset( $_SESSION['name'] );
unset( $_SESSION['email'] );
unset( $_SESSION['age'] );
header( sprintf('Location: %s', $_SERVER['SCRIPT_NAME'] ) );
}
/*
as each form only submits two values ( submit being one ) we can
simply remove it from the POST array and use the remaining field/value
to generate the session variables using simple array techniques
The below could easily be replaced with pre-defined variables, such as:
if( isset( $_POST['name'] ) )$_SESSION['name']=$_POST['name']; etc
*/
if ( isset( $_POST['submit'] ) ) {
/* remove the submit button & value from the array */
unset( $_POST['submit'] );
/* there will now be one item, with index = 0 */
$keys=array_keys( $_POST );
$values=array_values( $_POST );
/* set the session variable */
$_SESSION[ $keys[0] ]=$values[0];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method='POST'>
<?php
if( empty( $_SESSION['name'] ) ){
echo "<input type='text' name='name' placeholder='enter name' />";
}
if( !empty( $_SESSION['name'] ) && empty( $_SESSION['email'] ) ){
echo "<input type='text' name='email' placeholder='enter email address' />";
}
if( !empty( $_SESSION['name'] ) && !empty( $_SESSION['email'] ) && empty( $_SESSION['age'] ) ){
echo "<input type='text' name='age' placeholder='enter your age' />";
}
if( empty( $_SESSION['age'] ) ){
echo "<input type='submit' name='submit' value='Next'>";
} else {
if( empty( $_POST['finish'] ) ) echo "<input type='submit' name='finish' value='Finish'>";
}
if( isset( $_POST['finish'] ) ){
/*
Once the user has completed each form, send them off to their final destination?
or do something else....
*/
printf(
'Do some interesting stuff now ....
<pre>%s</pre><a href="?reset=true">Reset</a>',
print_r( $_SESSION, true )
);
}
?>
</form>
</body>
</html>