duanaoreng9355 2017-06-01 20:07
浏览 49
已采纳

使用会话在php页面之间传输数据

I am trying to transfer data between php pages using session. My code is as below:

check_code.php:

<?php
session_start();
echo "<form action=\"check_code.php\" method=\"post\">";

echo "<h2>Your Name.  *</h2><input type='text' name='user_name'>";
echo "<br><br>";
echo "<h2>Your age.  *</h2><input type='text' name='age'>";
echo "<br><br>";
echo "<br><br><br>";
echo "<div><input type='submit' value='Review'></div>";
echo "</form>";
?>

<?php  
if((empty($_POST['user_name'])) || (empty($_POST['age'])) ) {
    echo "<h2>Please enter your user name and age</h2>";
} else {
    echo "<form action=\"page2.php\" method=\"post\">";
    $user_name = $_POST['user_name'];
    $age = $_POST['age'];
    echo "Below are the details entered:<br>";
    echo "Name: $user_name";
    echo "Age: $age";
    echo "Select any one: ";
    echo '<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox" 
      name="subject[]" value="Science">Science</td>';
    echo '<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox" 
      name="subject[]" value="Math">Math</td>';
    echo "<br>";

    $_SESSION['user'] = $_POST['user_name'];
    $_SESSION['age'] = $_POST['age'];
    $_SESSION['subject'] = $_POST['subject'];


    echo "<input type=\"submit\" value='Add to DB' >";
    echo "</form>";
}   

?>

page2.php:

<?php
session_start();
$user_name =  $_SESSION['user'];
$age = $_SESSION['age'];
$subject = $_SESSION['subject'];
echo "<h2>Below are the details entered:</h2><br>";
echo "<h2>Name: </h2>$user_name";
echo "<h2>Age: </h2>$age";
echo "<h2>Subject selected: </h2>";
for ($i=0;$i<sizeof($subject);$i++) {
    echo "  $subject[$i]  ";
} 

?>

The name and age get displayed in the final page (page2.php). The subject does not get passed to the next page. What mistake am I making here?

Any help would be appreciated!!!

  • 写回答

1条回答 默认 最新

  • doumi1884 2017-06-01 21:24
    关注

    The code you gave had some issues, so I rewrote your code and you might try the one below:

    check_code.php file:

    <?php session_start(); ?>
    <form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
        <label>Your name</label>
        <input type="text" name="name" />
        <br>
        <label>Your age</label>
        <input type="number" name="age" />
        <hr>
        <button type="submit" name="review">Review</button>
        <?php if(isset($_SESSION['details'])) { ?>
        <button type="submit" name="unset">Unset</button>
        <?php } ?>
    </form>
    <?php
        if(isset($_SESSION['details'])) {
            if(isset($_POST['unset'])) { // If pressed "unset", remove the session and the values and restart
                unset($_SESSION);
                session_destroy();
            }
        }
    
        if(isset($_POST['review'])) {
            if(!empty($_POST['name']) && !empty($_POST['age'])) { // If fields are not empty
    ?>
    <p>Your Details:</p>
    <table>
        <tr>
            <td>Name<td>
            <td><?php echo $_POST['name']; ?></td>
        </tr>
        <tr>
            <td>Age<td>
            <td><?php echo $_POST['age']; ?></td>
        </tr>
    </table>
    <?php
                $_SESSION['details'] = array(
                    'name' => $_POST['name'],
                    'age'  => $_POST['age']
                    // Storing them in array as $_SESSION['details'][name/age/whatever]
                );
            }
            else {
                echo 'Please fill in the fields.';
            }
        }
    
        if(isset($_SESSION['details'])) {
    ?>
    <p><?php echo $_SESSION['details']['name']; /* Stored name in session */ ?>, Please Select Subject:</p>
    <form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
        <label>Science</label>
        <input type="checkbox" name="subject[]" value="science" />
        <br>
        <label>Math</label>
        <input type="checkbox" name="subject[]" value="math" />
        <hr>
        <button type="submit" name="send">Remember My Choice</button>
    </form>
    <?php
            if(isset($_POST['send'])) { // If you send the second form, then...
                if(isset($_POST['subject'])) { // If selected subject
                    $_SESSION['subject'] = array();
                    for($i = 0; $i < count($_POST['subject']); $i++) {
                        $_SESSION['subject'][] = $_POST['subject'][$i]; // store all values of "subject" in the session
                    }
                }
    
                header('location: page2.php');
            }
        }
    

    Explanation:

    You wanted the user to choose a subject after submitting the form, and defined it when the user can not check subject - line 33. when the user can not define the variable, you can continue - but with errors - and that's what I got when I tried your code.

    So what I did was the following steps:

    1. Send the first form with the name and the age
    2. Define $_SESSION variable named "details" (as array that held the required information)
    3. If this variable exists - then allow the user to select a subject.

    Then, when you choose one (or more) subjects, they're saved too in the session:

    page2.php file:

    <?php
        session_start();
        if(isset($_SESSION['details'])) {
    ?>
    <p>Your name is <?php echo $_SESSION['details']['name']; ?> and your age is <?php echo $_SESSION['details']['age']; ?></p>
    <?php if(isset($_SESSION['subject'])) { ?>
    <p>And your subject(s) are <?php echo implode(', ', $_SESSION['subject']); ?></p>
    <?php } else { ?>
    <p>You don't have any subject</p>
    <?php
            }
        }
        else {
            die('An Error Occurred');
        }
    

    On page2.php I checked if the details are set. if they are, then we can proceed and check if the subject(s) are set too. In case details are not set, the connection will die and print an error message. If you don't set the subject, you'll get a message about it, too.

    Important note:

    • Your code, and this one too are vulnerable. Do not use these in a server, unless you take care about XSS protection. You may escape characters and use Regular expressions to "Sanitize" the input.

    You can replace your current code with this one.

    I hope it will be helpful

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog