【问题标题】:Dynamic php form动态php表单
【发布时间】:2013-05-02 17:28:28
【问题描述】:

我正在尝试根据用户是“新用户”还是“现有用户”来更改我的登录表单。例如,如果选择“现有用户”仅显示“用户名”和“密码”输入字段。选择“新用户”会得到“用户名”、“电子邮件”、“密码”和“密码确认”。

<select id="login_type" name="logintype">
 <option value="Register">a new user</option> 
 <option value="Login">an existing user</option>
</select> 

尝试了下面的代码,但是当它在 if else 中读取我的 html 表单时卡住了。我能做什么?

<?php
if (isset($_POST['login_type']) && $_POST['login_type'] == 'an existing user') 
{ 
<div>
    <label for="uname_input">Username:</label>
    <input id="uname_input" name="uname" type="text" required="required" placeholder="Enter your username"/>
    <br />
    <label for="pass_input">Password:</label>
    <input id="pass_input" name="passwd" type="password" required="required" placeholder="Enter your password"/>
</div> 

<input id="submit_login" type="submit" value="Submit" /> 

} 
else 
{ 
    <div class="shownew">
        <label for="em_input">Email:</label>
        <input id="em_input" name="email" type="text"  placeholder="Enter a valid email address"/>
    </div>
    <div>
        <label for="uname_input">Username:</label>
        <input id="uname_input" name="uname" type="text" required="required" placeholder="Enter your username"/>
        <br />
        <label for="pass_input">Password:</label>
        <input id="pass_input" name="passwd" type="password" required="required" placeholder="Enter your password"/>
    </div>
    <div class="shownew">
        <label for="pass2_input">Confirm:</label>
        <input id="pass2_input" name="passwd2" type="password" placeholder="Repeat your password"/>
    </div> 
    <input id="submit_login" type="submit" value="Submit" /> 
}

if (isset($_GET['error']))
{
  $errmsg = '';
  switch ($_GET['error'])
  {
  case 1: $errmsg = 'Passwords entered do not match one another.'; break;
  case 2: $errmsg = 'Username already exists in the database. Please choose   a           different username.'; break;
  case 3: $errmsg = 'The username or password you entered is incorrect. Please try again.'; break;
  case 4: $errmsg = 'Invalid login mode. Please reload the page and try again.'; break;
  case 5: $errmsg = 'Unexpected error processing login. Please try again'; break;
  default: $errmsg = 'An unknown error occurred. Please try again in a few minutes.'; break;
  }
  print '<p class="errmsg">' . $errmsg . '</p>';
}
?>

【问题讨论】:

  • 你没有回应任何东西
  • 它必须是$_POST['logintype'] 而不是$_POST['login_type'](名称很重要)以及RegisterLogin 而不是a new useran existing user

标签: php forms dynamic


【解决方案1】:

看起来您缺少 php 开始/结束标签:

if (isset($_POST['login_type']) && $_POST['login_type'] == 'an existing user') 
{ 
<div>

应该是

if (isset($_POST['login_type']) && $_POST['login_type'] == 'an existing user') 
{
?> 
<div>

等等……

【讨论】:

    【解决方案2】:

    在isset中选择名称是'logintype'并忘记关闭)

    改变

    <?php
    if (isset($_POST['login_type']) && $_POST['login_type'] == 'an existing user') 
    { 
    

    <?php
    if (isset($_POST['logintype']) && $_POST['logintype'] == 'an existing user') 
    { 
    

    【讨论】:

      【解决方案3】:
      <select id="login_type" name="logintype">
        <option value="Register">a new user</option> 
        <option value="Login">an existing user</option>
      </select> 
      

      登录类型为RegisterLogin。描述与这里无关。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-12
        • 2015-08-18
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 2021-01-26
        • 2017-11-28
        相关资源
        最近更新 更多