0

在顶部,我有一个会话开始,并且会话 ID 可以在所有网页之间传递,因为所有网页的 ID 都是相同的,这还包括一个名为“用户名”的会话变量,其值为 Guest。我想将此变量更改为表单中输入的变量。这是我的第一篇文章,如有错误请见谅。

<form class="form1" method="post" action="./" id="form1">
<fieldset>
    <ul>
            <p>Please enter your username to continue to the webshop.</p>
            <label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>

            <input  class="submit .transparentButton" value="Next" type="submit" name="Submit"/> 

    </ul>
    <br/>
    </fieldset>
</form>

<?
if (isset($_POST['Submit'])) {
     $_SESSION['username'] = $_POST['username'];
}
?> 

谢谢

4

1 回答 1

4

您需要./在下面的action=""引号中取出(这是因为您使用相同的文件来处理表单)......并且总是开始您的 php 打开<?php

<form class="form1" method="post" action="" id="form1">
<fieldset>
    <ul>
            <p>Please enter your username to continue to the webshop.</p>
            <label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>

            <input  class="submit .transparentButton" value="Next" type="submit" name="Submit"/> 

    </ul>
    <br/>
    </fieldset>
</form>

<?php
if (isset($_POST['Submit'])) {
     $_SESSION['username'] = $_POST['username'];
}
?> 

如果您想对其进行测试,请尝试以下操作:

<?php
if (isset($_POST['Submit'])) {
    $_SESSION['username'] = $_POST['username'];

    // Use the following code to print out the variables.
    echo 'Session: '.$_SESSION['username'];
    echo '<br>';
    echo 'POST: '.$_POST['username'];
}
?> 

进行更改后测试代码,它工作正常......看:

测试截图


根据评论中的请求更新答案

<form class="form1" method="post" action="./" id="form1">
    <fieldset>
       <ul>
            <p>Please enter your username to continue to the webshop.</p>
            <label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>

            <input  class="submit .transparentButton" value="Next" type="submit" name="Submit"/> 

       </ul>
       <br/>
    </fieldset>
</form>

把它放在你的 index.php 中

<?php
    if (isset($_POST['Submit'])) {
        $_SESSION['username'] = $_POST['username'];

        // Use the following code to print out the variables.
        echo 'Session: '.$_SESSION['username'];
        echo '<br>';
        echo 'POST: '.$_POST['username'];
    }
?> 
于 2013-11-14T20:30:30.683 回答