如何使用session在php中传输输入数据

时间:2017-09-01 21:35:12

标签: php html

我正在处理这个项目,我需要帮助,在一个页面上显示用户输入的内容,并使用session在另一个页面上显示。

这是我的用户输入姓名的页面代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <form action="s2q2.php" name="s2n" method="POST">
        <p>Enter your name:</p>
        <input style="border: none; border-bottom: dashed; height: 50px; width:25%; margin-top: 45px; font-size: 35px;" type="text" script="sessionStorage.setItem('s2n');"><br>
        <button class="button" type="submit" style="vertical-align: center; background-color: #1e00ff; margin-top: 50px;" name="s2q1"><span>Next </span></button>
    </form>

</body>
</html>

4 个答案:

答案 0 :(得分:0)

您的问题有点令人困惑,但根据我使用PHP的经验,我们$_POST数据的使用情况以及$_SESSION 的简单方法

编辑:在您的问题中,我们不知道您是否知道如何使用session_start()。我们也不知道如何设置程序中的文件和文件夹以提供正确的答案。但通常(好吧)我创建一个保存数据库信息的config.php文件,然后我将该文件包含在标题(header.php)中,因为程序中的所有地方都包含header.php。假设您使用的是mysqli,我创建了文件示例。

<强>的config.php

if (session_status() == PHP_SESSION_NONE) {
 define('DB_SERVER', 'localhost');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'root');
   define('DB_DATABASE', 'my_database_name');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
}

    // This tells the web browser that your content is encoded using UTF-8 
    // and that it should submit content back to you using UTF-8 
    header('Content-Type: text/html; charset=utf-8'); 

    // This initializes a session.  Sessions are used to store information about 
    // a visitor from one web page visit to the next.  Unlike a cookie, the information is 
    // stored on the server-side and cannot be modified by the visitor.  However, 
    // note that in most cases sessions do still use cookies and require the visitor 
    // to have cookies enabled.  For more information about sessions: 
    // http://us.php.net/manual/en/book.session.php 
    session_start(); 

    // Note that it is a good practice to NOT end your PHP files with a closing PHP tag. 
    // This prevents trailing newlines on the file from being included in your output, 
    // which can cause problems with redirecting users.

header.php(在您需要的任何地方都包含此文件,或者包含config.php)

 header('Content-Type: text/html; charset=iso-8859-1'); 
 ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);//handle php errors
 ob_start();//prevent header from being sent twice
 require_once('config.php'); 

<强>形式

require_once('header.php'); 

<form action="s2q2.php" method="POST">
    <p>Enter your name:</p>
    <input type="text" name="name"><br>
    <button type="submit" name="s2q1"><span>Next </span></button>
</form>

<强> s2q2.php

if (isset($_POST['s2q1'])) {     // Retrieve form
    $name = $_POST['name'];      // retrieve data name
    $_SESSION['name'] = $name;   // pass data in session
}

使用$_SESSION

if (isset($_SESSION['name'])) {
    echo $_SESSION['name'];
} else {
    //do something else;
}

答案 1 :(得分:0)

首先,修改表单输入以具有名称:

我正在处理这个项目,我需要帮助,在一个页面上显示用户输入的内容,并使用session在另一个页面上显示。

这是我的用户输入姓名的页面代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

     <form action="s2q2.php" name="s2n" method="POST">
         <p>Enter your name:</p>
         <input name="username" style="border: none; border-bottom: dashed; height: 50px; width:25%; margin-top: 45px; font-size: 35px;" type="text" script="sessionStorage.setItem('s2n');"><br>
         <button class="button" type="submit" style="vertical-align: center; background-color: #1e00ff; margin-top: 50px;" name="s2q1"><span>Next </span></button>
    </form>
</body>
</html>

然后,在s2q2.php页面上:

<?php
session_start();

$_SESSION["username"] = $_POST["username"];
echo $_SESSION["username"] ;
?>

答案 2 :(得分:0)

首先,您创建一个名为 session_start.php 的会话文件,如下所示

 <?php
   session_start();
 ?> 

然后你只需要将它包含在想要拥有会话变量的任何地方

<!DOCTYPE html>
<html>
<head>
    <title>Input Site</title>
    <style>
    .input1 {
            border: none;
            border-bottom: dashed;
            height: 50px; width:25%;
            margin-top: 45px;
            font-size: 35px;
           }
    .button1 {
            vertical-align: center;
            background-color: #1e00ff;
            margin-top: 50px;
           }
    </style>
</head>
<body>

<form action="" method"post">
<p>Enter your name:</p>
<input class="input1" type="text" name="nameInput"><br>
<button class="button1" type="submit" name="submitBut"><span>Next</span></button>
</form>

<?php 
// including the session file
require_once("session_start.php");

if (isset($_POST['submitBut'])) { 
 $_SESSION['nameInput'] = $_POST['nameInput'];
} 
?> 
</body>
</html>

然后你创建任何网站 whatever.php ,并且只包含你的 session_start.php 文件

<!DOCTYPE html>
<html>
<head>
    <title>Whatever Site</title>
</head>
<body> 
<?php 
// including the session file
require_once("session_start.php");  
?>  
<strong><?php echo $_SESSION['nameInput'];?></strong>
</body>
</html>

答案 3 :(得分:0)

哦,好吧,让我们看另一个例子(需要在输出之前清理$ name!)

function refreshContent() {
    if( $('#edit-col').length == 0 )
    {
        $.ajax({
            type: "GET",
            url: "content.php",
            dataType: 'text',
            async: true,
            success: function(data) {    
                var contentArr = ["#container", "#progress-container", "#project-progress"];
                var arrayLength = contentArr.length;
                for (i = 0; i < arrayLength; i++) { 
                    var el = $(data).find(contentArr[i]);
                    if( $(contentArr[i]).html() != el.html() )
                    {
                        $(contentArr[i]).replaceWith(el);
                    }
                }       
            }
        });
    }
}

var starttime = 5000;
var time = starttime;
var maxtime = 320000;
var idleInterval;
$(document).ready(function () {

    idleInterval = setInterval(timerIncrement, time);

    $(this).on('mousemove', function (e) {
        clearInterval(idleInterval);
        idleInterval = setInterval(timerIncrement, time);
    });
    $(this).on('click', function (e) {
        clearInterval(idleInterval);
        idleInterval = setInterval(timerIncrement, time);
    });
    $(this).on('keypress', function (e) {
        clearInterval(idleInterval);
        idleInterval = setInterval(timerIncrement, time);
    });
});

function timerIncrement() {

    console.log('refreshContent is called after '+time+' milliseconds');
    refreshContent();

    time *= 2;
    if( time > maxtime ) { time = maxtime; }
    clearInterval(idleInterval);
    idleInterval = setInterval(timerIncrement, time);
}