链接两页

时间:2018-04-29 07:40:48

标签: javascript php login

我正在尝试将页面链接在一起,所以如果一个人使用登录页面登录,他们将被重定向到特定的创建页面,如果用户单击注册按钮,他们将被重定向到注册表页面并从那里输入信息后,它们将被定向到另一页面。登录或注册按钮不起作用。有没有办法来解决这个问题?

到目前为止,我的登录页面已经存在。

function login() {
  var users = ["admin1", "admin2", "admin3", "admin4"];
  var pass = ["pass1", "pass2", "pass3", "pass4"];

  ivar aUser = document.getElementById("user_name").value;
  var aPass = document.getElementById("password").value;

  for (i = 0; i < users.length; i++) {
    if (users[i] == aUser && pass[i] == aPass) {
      window.location = "dashboard.php";
      break;
    }
  }

  var myOut = document.getElementById("output");
  myOut.innerHTML = "Who Are You? / Incorrect Password";
  myOut.className = "error";

  function init() {
    var login_button = document.getElementById("login_button");
    if (login_button !== null) {
      login_button.onclick = login;
    }

    var register_button = document.getElementById("register_button");
    if (register_button !== null) {
      register_button.onclick = validation;
    }

    for (i = 1; i <= 10; i++) {
      var myErr = document.getElementById("err" + i);
      if (myErr !== null) {
        myErr.className = "error";
      }
    }
  }
<h1>
  Please Login
</h1>
<form id="order_form">
  <fieldset>
    <legend>Login</legend>
    <div class="tab">
      <div class="tRow">
        <div class="tRow">
          <div class="tCell">
            <label for="user_name">User Name:</label>
          </div>
          <div class="tCell">
            <input type="text" id="user_name" maxlength="50" />
          </div>
        </div>
        <!-- END OF THIS SELECTION -->
        <div class="tRow&gt; &lt;div class=">
          <label for="password">Password:</label>
        </div>
        <div class="tCell">
          <input type="text" id="password" required="" maxlength="50" />
        </div>
      </div>
      <!-- END OF THIS SELECTION -->
      <div class="tRow">
        <div class="tCell">
          &nbsp;
        </div>
        <div class="tCell">
          <input type="button" id="login_button" value="Login" />
        </div>

      </div>
      <!--END OF THIS SELECTION-->
      <div class="tRow">
        <div class="tCell">
          &nbsp;
        </div>
        <div class="tCell">
          <input type="button" id="register_button" value="Register Now!" />
        </div>

      </div>
      <!--END OF THIS SELECTION-->
    </div>
    <!-- END OF THE TABLE  -->
    <br />
    <div id="output" class="error"></div>
  </fieldset>
</form>

验证功能:

function validation() {

    for (i=1; i<=10; i++) {
      var myErr = document.getElementById("err" + i); 
      myErr.innerHTML = "";
   }
    document.getElementById("output").innerHTML = "";


        var dept_name = document.getElementById("dept_name").value;
        var user_email = document.getElementById("user_email").value;
        var user_password = document.getElementById("user_password").value;
        var phone_number = document.getElementById("phone_number").value;
        var first_name = document.getElementById("first_name").value;
        var last_name = document.getElementById("last_name").value;
        var office_location = document.getElementById("office_location").value;



        var valid = true;

        if (dept_name == "") {
            document.getElementById("err1").innerHTML = "Invalid!";
            valid = false;
        }

        if ((user_email) =="") {
        valid = false;
        document.getElementById("err2").innerHTML = "Invalid!"; 
        }

        if ((user_password) =="") {
        valid = false;
        document.getElementById("err3").innerHTML = "Invalid!"; 
        }

        if ((first_name) =="") {
        valid = false;
        document.getElementById("err4").innerHTML = "Invalid!"; 
        }

        if ((last_name) =="") {
        valid = false;
        document.getElementById("err5").innerHTML = "Invalid!"; 
        }

        if (isNaN(phone_number) || card_number.length !==11) {
        valid = false;
        document.getElementById("err6").innerHTML = "Invalid!"; 
        }

        if ((office_location) =="") {
        valid = false;
        document.getElementById("err7").innerHTML = "Invalid!"; 
        }
}
window.onload=init;

1 个答案:

答案 0 :(得分:0)

几个错误

  1. 缺少括号
  2. 没有执行init
  3. 缺少验证功能
  4. 这有效:

    &#13;
    &#13;
    function login() {
      var myOut = document.getElementById("output");
    
      var users = ["admin1", "admin2", "admin3", "admin4"];
      var pass = ["pass1", "pass2", "pass3", "pass4"];
    
      var aUser = document.getElementById("user_name").value;
      var aPass = document.getElementById("password").value;
    
      for (i = 0; i < users.length; i++) {
        if (users[i] == aUser && pass[i] == aPass) {
          myOut.innerHTML = "Correct - you will be redirected";
          setTimeout(function() {
            window.location = "dashboard.php";
          },1000);  
          return;
        }
      }
      myOut.innerHTML = "Who Are You? / Incorrect Password";
      myOut.className = "error";
    }
    
    
    function init() {
      var login_button = document.getElementById("login_button");
      if (login_button !== null) {
        login_button.onclick = login;
      }
    
      var register_button = document.getElementById("register_button");
      if (register_button !== null) {
        register_button.onclick = validation;
      }
    
      for (i = 1; i <= 10; i++) {
        var myErr = document.getElementById("err" + i);
        if (myErr !== null) {
          myErr.className = "error";
        }
      }
    }
    function validation() { /* you need some code here */ }
    window.onload=init;
    &#13;
    <h1>
      Please Login
    </h1>
    <form id="order_form">
      <fieldset>
        <legend>Login</legend>
        <div class="tab">
          <div class="tRow">
            <div class="tRow">
              <div class="tCell">
                <label for="user_name">User Name:</label>
              </div>
              <div class="tCell">
                <input type="text" id="user_name" maxlength="50" />
              </div>
            </div>
            <!-- END OF THIS SELECTION -->
            <div class="tRow&gt; &lt;div class=">
              <label for="password">Password:</label>
            </div>
            <div class="tCell">
              <input type="text" id="password" required="" maxlength="50" />
            </div>
          </div>
          <!-- END OF THIS SELECTION -->
          <div class="tRow">
            <div class="tCell">
              &nbsp;
            </div>
            <div class="tCell">
              <input type="button" id="login_button" value="Login" />
            </div>
    
          </div>
          <!--END OF THIS SELECTION-->
          <div class="tRow">
            <div class="tCell">
              &nbsp;
            </div>
            <div class="tCell">
              <input type="button" id="register_button" value="Register Now!" />
            </div>
    
          </div>
          <!--END OF THIS SELECTION-->
        </div>
        <!-- END OF THE TABLE  -->
        <br />
        <div id="output" class="error"></div>
      </fieldset>
    </form>
    &#13;
    &#13;
    &#13;

    您可以简化测试:

    var userPos = users.indexOf(aUser); 
    if (userPos !=-1 && userPos === pass.indexOf(aPass)) {
      ...
    }