簡體   English   中英

我試圖打印到屏幕上的這款手機 SESSION 有什么問題?

[英]What am I doing wrong with this phone SESSION I'm trying to print to screen?

我正在測試一個會話,其中前三頁打印到最后一頁。 但我很難讓它工作。 一切順利,直到最后一頁。 這是代碼:

<?php
session_start();

if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
$_SESSION['age'] = $_POST ['age'];

echo $_SESSION['name'];
echo $_SESSION['email'];
echo $_SESSION['age'];
}
?>

會話變量出現空白或索引錯誤。 任何人都可以幫助以正確的方式打印用戶輸入的會話數據嗎?

這些是第一、第二和第三頁:

<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_1.php">
<input type="text" name="name" placeholder="enter name"></input>
<input type="submit"> next</input>
</form>
<?php
//Set session variables
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
}
?>
</body>
</html>


<?php
session_start();
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_2.php">
<input type="text" name="email" placeholder="enter email"></input>
<input type="submit"> next</input>
</form>
<?php
// Set session variables
?>
</body>
</html>


<?php
session_start();
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
$_SESSION['age'] = $_POST ['age'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="post_test.php">
<input type="text" name="age" placeholder="enter age"></input>
<input type="submit"> next</input>
</form>
<?php
// Set session variables
?>
</body>
</html>

您的 HTML 中沒有名稱為submit input 元素,因此您的 PHP 代碼的以下部分將始終評估為false

if (isset($_POST['submit'])) {

將 HTML 中的提交按鈕更改為以下內容,當表單發布時,上面的行將評估為true

<input name="submit" type="submit"> next</input>

input元素是self-closing ,因為您不需要</input> - 而是使用/>簡單地關閉標簽

要將Next顯示為提交按鈕,您將使用 value 屬性 - 即: <input type='submit' value='Next' />

一旦分配了會話變量,您就不需要繼續嘗試設置它,如果該變量在 POST 數組中不可用,則會遇到錯誤 - 因此在創建變量之前使用isset進行測試

<?php
    session_start();
    if ( isset( $_POST['submit'], $_POST['name'] ) ) { 
        $_SESSION['name'] = $_POST['name'];
    }   
?>
<html>
    <head>
        <title>phpsession</title>
    </head>
    <body>
        <form method="POST" action="test_2_page_1.php">
            <input type="text" name="name" placeholder="enter name" />
            <input type="submit" name='submit' value='Next'>
        </form>
    </body>
</html>





<?php
    session_start();
    if ( isset( $_POST['submit'], $_POST['email'] ) ) {
        $_SESSION['email'] = $_POST['email'];
    }
?>
<html>
    <head>
        <title>phpsession</title>
    </head>
    <body>
        <form method="POST" action="test_2_page_2.php">
            <input type="text" name="email" placeholder="enter email" />
            <input type="submit" name='submit' value='Next'>
        </form>
    </body>
</html>







<?php
    session_start();
    if( isset( $_POST['submit'],$_POST['age'] ) ) {
        $_SESSION['age'] = $_POST['age'];
    }
?>
<html>
    <head>
        <title>phpsession</title>
    </head>
    <body>
        <form method="POST" action="post_test.php">
            <input type="text" name="age" placeholder="enter age" />
            <input type="submit" name='submit' value='Next'>
        </form>
    </body>
</html>

為了進一步幫助您解決問題,我快速整理了一個基於表單提交獲取和設置會話變量的單頁演示..希望它會有所幫助

<?php
    session_start();
    #https://stackoverflow.com/questions/54194496/what-am-i-doing-wrong-with-this-phone-session-im-trying-to-print-to-screen/54194890?noredirect=1#comment95217192_54194890

    if( !empty( $_GET['reset'] ) ){
        unset( $_SESSION['name'] );
        unset( $_SESSION['email'] );
        unset( $_SESSION['age'] );
        header( sprintf('Location: %s', $_SERVER['SCRIPT_NAME'] ) );
    }

    /* 
        as each form only submits two values ( submit being one ) we can
        simply remove it from the POST array and use the remaining field/value
        to generate the session variables using simple array techniques

        The below could easily be replaced with pre-defined variables, such as:
        if( isset( $_POST['name'] ) )$_SESSION['name']=$_POST['name']; etc

    */
    if ( isset( $_POST['submit'] ) ) {
        /* remove the submit button & value from the array */
        unset( $_POST['submit'] );

        /* there will now be one item, with index = 0 */
        $keys=array_keys( $_POST );
        $values=array_values( $_POST );

        /* set the session variable */
        $_SESSION[ $keys[0] ]=$values[0];
    }

?>
<html>
    <head>
        <title>phpsession</title>
    </head>
    <body>
        <form method='POST'>
        <?php
            if( empty( $_SESSION['name'] ) ){
                echo "<input type='text' name='name' placeholder='enter name' />";
            }
            if( !empty( $_SESSION['name'] ) && empty( $_SESSION['email'] ) ){
                echo "<input type='text' name='email' placeholder='enter email address' />";
            }
            if( !empty( $_SESSION['name'] ) && !empty( $_SESSION['email'] ) && empty( $_SESSION['age'] ) ){
                echo "<input type='text' name='age' placeholder='enter your age' />";
            }
            if( empty( $_SESSION['age'] ) ){
                echo "<input type='submit' name='submit' value='Next'>";
            } else {
                if( empty( $_POST['finish'] ) ) echo "<input type='submit' name='finish' value='Finish'>";
            }
            if( isset( $_POST['finish'] ) ){
                /* 
                    Once the user has completed each form, send them off to their final destination?
                    or do something else....
                */
                printf(
                    'Do some interesting stuff now ....
                    <pre>%s</pre><a href="?reset=true">Reset</a>',
                    print_r( $_SESSION, true )
                );
            }
        ?>
        </form>
    </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM