2

Creating form and trying to carry variables accross two pages to a results page. I have tried $GET and $POST and it works fine from page1.php to results.php, but when I change to $SESSION the variable isn't passed or echo'd on results.php. Here's the php code for page1 which won't even work directly to results! Not sure if there is a problem with my code or possibly the server? Page 1.php:

<?php session_start();?>
<?php
$name = $_SESSION['name']; 
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />

Results.php:

<?php session_start();?>

<html>           
    <body>                   
        <?php 
        $name = $_SESSION['name'];
        echo $name; ?>                               
    </body>           
</html> 
4

6 回答 6

2

请尝试执行以下代码片段

<?php session_start();?>
<?php
   if($_SERVER['REQUEST_METHOD']=='POST')
   {
    $name = $_POST['name'];
    $_SESSION['name']=$name;
  }
 ?>
  <FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
  <input type="text" name="name" id="name" />

实际上在您的代码片段中 $_SESSION['name'] 的值没有设置。所以我已经定义了会话变量的值,并从 HTML 表单中发布了值

于 2013-04-23T08:46:24.810 回答
2

您忘记了表单中的结束标签,并且没有提交按钮

<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />   
<input type="Submit" value="Submit">
</FORM>

然后在 results.php

<?php session_start();?>

<html>           
    <body>                   
        <?php 

        $_SESSION['name']=$_POST['name'];
        echo  $_SESSION['name']; ?> 
        <br><br>
        <a href="page2.php">page 2</a>  


    </body>           
</html> 

然后我创建了这个 page2.php

<?php session_start();?>
<html>
    <body>
        Hi, I am still <?php echo $_SESSION['name'];?>
    </body>
</html>

在我身边一切都很好。

于 2013-04-23T08:51:38.180 回答
0

我认为你在这一行反转了你的变量:

<?php
 $name = $_SESSION['name']; 
?>

你可能想做:

<?php
$_SESSION['name'] = $name;
?>
于 2013-04-23T08:44:19.787 回答
0

首先,您将名称变量分配给会话变量

<?php session_start();?>
<?php
$_SESSION['name'] = $name;
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
于 2013-04-23T08:45:27.267 回答
0

结果.php:

<?
session_start();
$name = isset($_POST['name'])?$_POST['name']:'';

if($name){
$_SESSION['name']=$name;

}
?>
<html>           
<body>                   
    <?php echo $name; ?>                               
</body>           
</html> 
于 2013-04-23T08:46:37.787 回答
0
Try....
 Page 1.php : <?php session_start();
$_SESSION['name'] = isset($_POST['name']) ? $_POST['name'] : '';
 ?>
  <FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
  <input type="text" name="name" id="name" />

Results.php:

<?php session_start();?>

<html>           
    <body>                   
        <?php 
        $name = $_SESSION['name'];
        echo $name; ?>                               
    </body>           
</html>
于 2014-05-06T09:28:27.527 回答