dongque4778 2014-08-07 02:57
浏览 6
已采纳

Php会话无法处理输入

I have this code that requires the user to type in a number and that number of lessons will be created.

<form action="k1levelselect.php" method="post"> How many lessons do you want to create?
<input type="text" name="number" value=""><br>
<input type="submit" value="Submit">
</form>

<?php
session_start();
$_SESSION['views']=$number;
?>

Then it will bring me to the next page and prints out the number of 'Lessons' the user wants.

 <?php
    session_start();

    $noOfLesson = $_SESSION['views'];
        for($i = 1; $i <= $noOfLesson; $i++) {
        echo "Pageviews=". $_SESSION['views'];
    echo "<div>
        <a href=\"k1levelX.php?lesson=".$i."\"><span>Lesson ".$i."</span></a>           
    </div>
    <br>";
        }
        ?>

The problem is in the first code, i am not able to bring the input 'number' over to the session using $_SESSION['views']=$number; However, it works when i hardcode a number like eg. $_SESSION['views']=4;

  • 写回答

2条回答 默认 最新

  • du4010 2014-08-07 04:11
    关注

    Foreword: (edit)
    I noticed you've asked a few questions since first joining Stack but have not accepted any.

    If there is a reason as to why you haven't accepted any, probably because none of the answers given solved an issue, do inform the person or persons to elaborate on their answer.


    Answer:

    Here, this is what you need to do, in regards to your comment about it "not working": (plus, you weren't assigning a variable in regards to your POST form element). This has already been explained.

    Your (new) form: - Make sure there is nothing above this (no space, cookie, HTML, nothing), because it will cause/throw a "Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at..." error; if error reporting is ON by default.

    <?php
    
    session_start();
    
    // check if session is set, which will echo the last entered number
    if(isset($_SESSION['views'])){
    echo $_SESSION['views'];
    }
    
    ?>
    
    <form action="k1levelselect.php" method="post">
    How many lessons do you want to create?
    <input type="text" name="number" value=""><br>
    <input type="submit" value="Submit">
    </form>
    

    Copy this into your k1levelselect.php file: - Make sure there is nothing above this (no space, cookie, HTML, nothing), because it will cause/throw a "Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at..." error; if error reporting is ON by default.

    <?php 
    
    session_start();
    $number = $_POST['number']; // assign variable from POST
    $_SESSION['views']=$number; // assign session to variable
    
        $noOfLesson = $_SESSION['views'];
            for($i = 1; $i <= $noOfLesson; $i++) {
            echo "Pageviews=". $_SESSION['views'];
        echo "<div>
            <a href=\"k1levelX.php?lesson=".$i."\"><span>Lesson ".$i."</span></a>           
        </div>
        <br>";
            }
    ?>
    

    If error reporting is not ON by default, add the following below your opening <?php tags:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    It will show you any errors; if any.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误