从表单设置 cookie (html/php)

Set cookie from form (html/php)

我有一个带有表单的 php 页面,我想从中搜索用户数据库以查找用户名匹配项。这是我的代码:

<form id="searchuser" name="searchuser" method="post" action="">
    Enter a username to search for: 
    <label>
        <input type="text" name="uname" id="uname" />
    </label>
    <label>
        <input type="submit" name="submit" id="submit" value="Search" />
    </label>
    <p>&nbsp;</p>
</form>

我需要在动作中加入 setcookie() 吗?

表单的 action 字段应包含 php 脚本的 URL(相对或绝对),浏览器将在将表单字段作为 POST请求。

例如,您可以创建一个名为 search.php 的 php 脚本,在表单中设置 action="search.php",然后使用 [=13= 访问该脚本中表单的字段] 例如。

不确定您需要为此表单设置 cookie 的内容,但要设置 cookie,您需要将 setcookie() 调用放在 php 脚本中的某处,而不是 HTML代码。您还需要在 php 脚本中的任何 HTML 输出之前调用 setcookie()。