0

目前登录的用户必须手动输入他们的姓名才能发表评论或发帖,但我想在自动插入的 html 表单的字段名称中获取用户名。同时用户也不能更改名称字段。

我如何在用户名字段中创建这样的会话,我试过但代码不起作用..

像这样

//create the form to submit comments
//you can add more fields, but make sure you add them to the db table and the page, submitcomment.php
    echo "
<a name=\"post\">
<div id=\"submitcomment\" class=\"submitcomment\">
<form name=\"submitcomment\" method=\"post\" action=\"submitcomment.php\" onSubmit=\" return form_Validator(this)\">
<table width=\"100%\">
        <tr>
                <th colspan=\"2\"><h4><span>Leave your comment:</span></h4></th>
        </tr>

              <th scope=\"row\"><p class=\"req\">Name:</p></th>
              <td><input type= class=\"form\" tabindex=\"1\" id=\"name\" name=\"name\" /></td>
      </tr>
        <tr>
                <th scope=\"row\"><p class=\"opt\">Email:</p></th>
                <td><input class=\"form\" tabindex=\"2\" id=\"email\" name=\"email\" /></td>
        </tr>
        <tr>

                <th scope=\"row\"><p class=\"opt\">URL:</p></th>
                <td><input class=\"form\" tabindex=\"3\" id=\"url\" name=\"url\" /></td>
        </tr>
        <tr valign=\"top\">
                <th scope=\"row\"><p class=\"req\">Comments:</p><br /></th>
                <td><textarea class=\"formtext\" tabindex=\"4\" id=\"message\" name=\"message\" rows=\"10\" cols=\"50\"></textarea></td>
        </tr>

        <tr>    
                <td>&nbsp;</td>
                <td><input type=\"submit\" name=\"post\" class=\"submit\" value=\"Submit Comment\" /><br />
                <p>Note:  Emails will not be visible or used in any way, and are not required.  Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted. </p>
                <p>In Beta Phase</p>

</td>
        </tr>
</table>
<input type=\"hidden\" name=\"tuturl\" value=\"$tuturl\" />
<input type=\"hidden\" name=\"tutid2\" value=\"$tutid2\" />
</form>


</div>
";
}
?>
4

2 回答 2

0
<?php
session_start();

// after user logs in successfully

$_SESSION['username']=$username;

// to read it
$username=$_SESSION['username'];
// then store $username in database when inserting comment

?>
于 2013-01-16T09:06:20.227 回答
0

尝试在要自动添加名称的页面上使用以下代码。

    <?php
    session_start();
    $_SESSION['name']=$name;
    $name=$_SESSION['name'];  // assign session to name variable
?>

在要显示名称的任何位置写入 $name。

然后编辑您的以下代码行

   <td><input type= class=\"form\" tabindex=\"1\" id=\"name\" name=\"$name\" readonly=\"readonly\" /></td>
于 2013-01-16T09:52:46.240 回答