0

我正在尝试使用 php 会话将输入值从一个页面传递到另一个页面。花了整个晚上试图解决这个问题,但没有结果。愿意你能帮助我。这是我的 php 脚本:

   <?php 
    $dbc = mysqli_connect ('localhost', 'root', 'root', 'db') or die ('Error connecting');
    $lt = $_POST ['lt'];
    $lg = $_POST ['lg'];
    $loc= $_POST ['loc'];
    $query = "INSERT INTO locations (lt, lg, loc)".
    "VALUES ('$lt', '$lg', '$loc')";
    mysqli_query ($dbc, $query)
    or die ('Error querying');
    header('Location: http://localhost:8888/stream.php');
    mysqli_close ($dbc);
    session_start();
    $_SESSION['cl'] = $_POST['loc'];
    ?>

和 HTML,应该传递取值:

<li class="currentLocation">
     <input class="currentLocationField" name="currentLocationField" type="text" value="<?php      echo $_SESSION["cl"] ?>" />
</li>

非常感谢您提前。

4

1 回答 1

0

只需重新排序最后四行,您就可以开始了:

header('Location: http://localhost:8888/stream.php');
mysqli_close ($dbc);
session_start();
$_SESSION['cl'] = $_POST['loc'];

进入这个:

session_start(); // even better to put this at the very top of the page
$_SESSION['cl'] = $_POST['loc'];
mysqli_close ($dbc);
header('Location: http://localhost:8888/stream.php');

并且不要忘记在您的登录页面上打开会话。

于 2013-07-11T22:04:58.800 回答