【问题标题】:cannot set a variable to base on user input post method无法将变量设置为基于用户输入发布方法
【发布时间】:2012-04-07 11:21:12
【问题描述】:

您好,我正在尝试将来自 post 方法的信息设置为一个变量,该变量将一个名为 name 的会话设置为用户输入的值。我收到以下错误:

注意:未定义索引:第 18 行 F:\xampp\htdocs\Impossible game\index.php 中的名称

这是第 18 行:$session = $_POST['name'];

<form action="ms1.php" method="POST">
Name <input type="text" name="name">
<input type="Submit" value="Begin">
</form>
<?php
$session = $_POST['name']; 
session_start();
$_SESSION['name'] = $session;

之前的错误消失了,现在出现了

注意:未定义变量:F:\xampp\htdocs\Impossible game\index.php 中第 23 行的 session 为第 23 行

 $_SESSION['name'] = $session;

【问题讨论】:

    标签: php


    【解决方案1】:

    以下代码已注释但未经测试。如果他们有任何问题,请告诉我。

        <?php
        if(isset($_POST['name'])){           //if the post has been set
            $session = $_POST['name'];       // Store the session in the variable
            session_start();
            $_SESSION['name'] = $session;    // Store the name in a session for later use
    header("location:index.php");    // Redirect the user     ##UPDATE##
        }   else    {
            //if post has not been set, show the form
        ?>
        <form action="ms1.php" method="POST">
        Name <input type="text" name="name">
        <input type="Submit" value="Begin">
        </form>
        <?php
        }
    

    【讨论】:

    • 代码有效,但它不会重定向,因为在 ms1.php 上我得到了pastie.org/3744452
    • 并显示 Notice: Undefined variable: _SESSION in F:\xampp\htdocs\Impossible game\ms1.php on line 3 on ms1.php
    • 这一行是第 3 行:if(!session_is_registered(ms3)){
    • 我已经更新了代码,以便它可以重定向,我的缩进已经搞砸了,因为我还在学习如何在 Stackoverflow 上正确显示它,我希望你能看过去。如果您有任何问题,请告诉我。
    • 在 ms1.php pastie.org/3745066987654322@
    【解决方案2】:

    你应该在使用它们之前检查变量、索引、偏移量是否设置。

    if (isset($_POST['name']))
    {
       $session = $_POST['name']; 
    }
    

    【讨论】:

      【解决方案3】:

      首先,HTML 部分和 PHP 部分是两个不同的东西:

      index.html

      <form action="ms1.php" method="POST">
            Name <input type="text" name="name">
            <input type="Submit" value="Begin">
      </form>
      

      ms1.php

      <?php
           if (isset($_POST['name']))
                   $session = $_POST['name']; 
      
           session_start();
           $_SESSION['name'] = $session;
      ?>
      

      【讨论】:

      • Ms1 代表任务 1,在索引 i 中,用户定义一个名称,在 ms 1 中,用户名称将回显。在任务 1 中有一个任务来解决它,只是我必须定义名称。
      猜你喜欢
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多