Shreyas Sreenivasa Shreyas Sreenivasa - 1 month ago 5
Javascript Question

How do I redirect my page on clicking submit?

I don't see what's wrong in my code down here. I want it to go to NHGSignin.php if 'new horizon gurukul' is typed. On the the click of the the Next button it is supposed to redirect me but it doesn't. I don't understand why? Thank you in advance!

Here's my code:



var schoolName = document.getElementById('schoolNameBox').value.toLowerCase();

function displaySignInError() {
switch (schoolName) {
case 'new horizon gurukul':
document.getElementById("schoolName").action = 'NHGLogin.php';
break;
default:
console.log('ajlsdfba');
}
}

@import url('https://fonts.googleapis.com/css?family=Catamaran:100,200,300,400,500,600,700,800,900|Droid+Sans:400,700|Josefin+Sans:100,100i,300,300i,400,400i,600,600i,700,700i|Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i|Oxygen:300,400,700|Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i|Ubuntu:300,300i,400,400i,500,500i,700,700i');

#container {
width: 30em;
background-color: #eee;
height: 30em;
border-radius: 50%;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transform: -webkit-translate(-50%, -50%);
transform: -ms-translate(-50%, -50%);
}

#wrapper {
width: 30rem;
height: 30rem;
border-radius: 50%;
}


#schoolSubmitButton {
margin-top: 35px;
text-align: center;
background-color: white;
border: 2px solid #fef;
height: 2em;
width: 10em;
}

#schoolName {
margin-bottom: 40px;
position: fixed;
top: 60%;
left: 50%;
transform: translate(-50%, -50%);
transform: -webkit-translate(-50%, -50%);
transform: -ms-translate(-50%, -50%);
text-align: center;
}

#schoolNameBox {
height: 2em;
border: none;
width: 26em;
margin-left: 0;
padding-left: 10px;
}

#schoolSubmitButton p {
position: relative;
top: 50%;
position: relative;
top: 30%;
left: 50%;
transform: translate(-50%, -70%);
transform: -webkit-translate(-50%, -70%);
transform: -ms-translate(-50%, -70%);
}

/*
::-webkit-input-placeholder {
padding-left: 10px;
}

:-moz-placeholder {
padding-left: 10px;
}

:-ms-input-placeholder {
padding-left: 10px;
}
*/

#signUpPageLink {
text-decoration: none;
position: relative;
top: 5em;
}

#schoolNameDiv {
position: fixed;
top: 60%;
left: 50%;
transform: translate(-50%, -50%);
transform: -webkit-translate(-50%, -50%);
transform: -ms-translate(-50%, -50%);
text-align: center;
}

#main-heading {
text-align: center;
text-decoration: none;
font-size: 24px;
}

#schoolAvatar {
height: 9em;
width: 9em;
border-radius: 50%;
position: fixed;
top: 25%;
left: 50%;
transform: translate(-50%, -50%);
transform: -webkit-translate(-50%, -50%);
transform: -ms-translate(-50%, -50%);
}

<!DOCTYPE html>
<html>
<head>
<title>NHG</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link type="text/css" rel="stylesheet" href="css/normalize.css"/>
<link type="text/css" rel="stylesheet" href="css/style.css"/>
<link type="text/css" rel="stylesheet" href="css/resposive.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<header>
<div id="main-head">
<!-- REMEMBER TO STYLE THE HEADING AND SIGN UP LINK -->
<a href="#" id="main-heading"><h2>sKoolBook</h2></a>
</div>
</header>
<section>
<div id="container">
<div id="wrapper">
<img src="https://i.imgsafe.org/a40bbe047e.png" alt="avatar" id="schoolAvatar" align="middle">
<div id="schoolNameDiv">
<form method="POST" id="schoolName" action="#">
<input type="text" name="schoolName" id="schoolNameBox" placeholder="Enter you School Name...">
<br>
<button type="submit" id="schoolSubmitButton" onclick="displaySignInError();">
<p>Next</p>
</button>
<br>
</form>
<br>
<a href="signUp.php" id="signUpPageLink">Don't have an account? Sign Up.</a>
</div>
</div>
</div>
</section>
<footer>
</footer>
<p id="demo"></p>
<script src="JS/script.js"></script>
</body>
</html>




Answer

From your answer I guess you're trying to change the action of the form.

When you do:

var schoolName = document.getElementById('schoolNameBox').value.toLowerCase();

Outside the function it only gets the schoolName when the html is rendered, if you want to get the schoolName when the user clicks the button you should do this:

function displaySignInError() {
  var schoolName = document.getElementById('schoolNameBox').value.toLowerCase();
  switch (schoolName) {
    case 'new horizon gurukul':
      document.getElementById("schoolName").action = 'NHGLogin.php';
      break;
    default:
      console.log('ajlsdfba');
  }
}

Check the updated snippet below.

function displaySignInError() {
  var schoolName = document.getElementById('schoolNameBox').value.toLowerCase();
  switch (schoolName) {
    case 'new horizon gurukul':
      document.getElementById("schoolName").action = 'NHGLogin.php';
      break;
    default:
      console.log('ajlsdfba');
  }
}
@import url('https://fonts.googleapis.com/css?family=Catamaran:100,200,300,400,500,600,700,800,900|Droid+Sans:400,700|Josefin+Sans:100,100i,300,300i,400,400i,600,600i,700,700i|Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i|Oxygen:300,400,700|Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i|Ubuntu:300,300i,400,400i,500,500i,700,700i');

#container {
  width: 30em;
  background-color: #eee;
  height: 30em;
  border-radius: 50%;
  position: fixed; 
  top: 50%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
}

#wrapper {
  width: 30rem;
  height: 30rem;
  border-radius: 50%;
}


#schoolSubmitButton {
  margin-top: 35px;
  text-align: center;
  background-color: white;
  border: 2px solid #fef;
  height: 2em;
  width: 10em;
}

#schoolName {
  margin-bottom: 40px;
  position: fixed; 
  top: 60%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
  text-align: center;
}

#schoolNameBox {
  height: 2em;
  border: none;
  width: 26em;
  margin-left: 0;
  padding-left: 10px;
}

#schoolSubmitButton p {
  position: relative;
  top: 50%;
  position: relative; 
  top: 30%; 
  left: 50%; 
  transform: translate(-50%, -70%);
  transform: -webkit-translate(-50%, -70%);
  transform: -ms-translate(-50%, -70%);
}

/*
::-webkit-input-placeholder {
   padding-left: 10px;
}

:-moz-placeholder {
  padding-left: 10px;
}

:-ms-input-placeholder {
  padding-left: 10px;
}
*/

#signUpPageLink {
  text-decoration: none;
  position: relative;
  top: 5em;
}

#schoolNameDiv {
  position: fixed; 
  top: 60%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
  text-align: center;
}

#main-heading {
  text-align: center;
  text-decoration: none;
  font-size: 24px;
}

#schoolAvatar {
  height: 9em;
  width: 9em;
  border-radius: 50%;
  position: fixed; 
  top: 25%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
}
<!DOCTYPE html>
<html>
  <head>
    <title>NHG</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link type="text/css" rel="stylesheet" href="css/normalize.css"/>
    <link type="text/css" rel="stylesheet" href="css/style.css"/>
    <link type="text/css" rel="stylesheet" href="css/resposive.css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  </head>
  <body>
    <header>
            <div id="main-head">
              <!--      REMEMBER TO STYLE THE HEADING AND SIGN UP LINK        -->
              <a href="#" id="main-heading"><h2>sKoolBook</h2></a>
            </div>
    </header>
    <section>
      <div id="container">
        <div id="wrapper">
          <img src="https://i.imgsafe.org/a40bbe047e.png" alt="avatar" id="schoolAvatar" align="middle">
          <div id="schoolNameDiv">
            <form method="POST" id="schoolName" action="#">
              <input type="text" name="schoolName" id="schoolNameBox" placeholder="Enter you School Name...">
              <br>
              <button type="submit" id="schoolSubmitButton" onclick="displaySignInError();">
                <p>Next</p>
              </button> 
              <br>
            </form>
            <br>
            <a href="signUp.php" id="signUpPageLink">Don't have an account? Sign Up.</a>
          </div>
        </div>
      </div>
    </section>
    <footer>
    </footer>
    <p id="demo"></p>
    <script src="JS/script.js"></script>
  </body>
</html>