PHP会话。将输入值从一个页面传递到另一个页面

时间:2013-07-11 21:18:54

标签: php session

我正在尝试使用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>

非常感谢您提前。

1 个答案:

答案 0 :(得分: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');

并且不要忘记打开目标网页上的会话。