理解HTML表单代码

时间:2014-07-08 06:40:16

标签: html forms

我对代码感到有点困惑。这是login.php文件..表单的动作也在同一个文件中。这可能发生吗?如果,如果用户提交表格,则应打开相同的登录表单...我正在建立我的网站,我想使用登录/注册表格。

<form action="login.php" method="post" class="f-wrap-1">
    <div class="req">
       <a href="signup.php">Not Registered?</a><br />
       <a href="recoverpass.php">Forgot your Password?</a>
    </div>
    <fieldset>
      <h3>Member Login</h3>
        <label for="firstname"><b>Username:</b>
           <input id="username" name="username" type="text" class="f-name" autocomplete="on" tabindex="1" /><br />
        </label>
        <label for="password"><b>Password:</b>
           <input id="password" name="password" type="password" class="f-name" autocomplete="off" tabindex="2" /><br />
        </label>
        <label for="code"><b>Security Code:</b>
           <input id="code" name="code" type="text" class="f-name" autocomplete="off" tabindex="3" /><br />
        </label>
        <label for="code2"><b>&nbsp;</b>
           <img src="image.php?" /><br />
        </label>
        <div class="f-submit-wrap">
           <input type="submit" value="Submit" class="f-submit" tabindex="4" /><br />
        </div>
    </fieldset>
</form>

2 个答案:

答案 0 :(得分:4)

  

这是login.php文件..表单的动作也在同一个文件中。这可能发生吗?

  

if,如果用户提交表单

,则应打开相同的登录表单

不一定。在这样的系统中,表单数据将由服务器端代码处理。逻辑可能类似于:

If it is a GET request, send the browser the form.

Otherwise, if it is a POST request, then check the form data:

    If it is valid login data, then: set a cookie to track the user and 
    tell the browser to get some other URL.

    Otherwise, the login data is wrong: populate the form with an error message
    and possibly default the values of the fields to the wrong data the user
    entered, then send the form to the browser.

使用无效数据填充表单在这样的登录表单中没有多大意义,但它在(例如)注册表单中更有用。

谁想重新输入所有个人数据 只是因为他们想要的用户名不可用或他们错过了某个字段?

答案 1 :(得分:0)

可以将表单发送到同一个文件但不是很好。您必须检查帖子数据是否可用并根据该数据显示正确的视图。

更好的方法是将表单的操作更改为指向另一个文件,并在该文件中处理登录逻辑。

<form action="file.php" method="post" class="f-wrap-1">