Is there a way to automatically redirect my page after clicking the submit button?

I'm having trouble with the code below. I want it to redirect to NHGSignin.php if 'new horizon gurukul' is entered. When I click the Next button, it's supposed to take me there but it's not working as expected. Can anyone help me figure out why? Thanks in advance!

Check out my code snippet:

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 №1

It seems like you're attempting to modify the form's action based on user input.

Instead of retrieving the schoolName value outside the function as:

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

You should capture the schoolName when the user clicks the button by using this function:

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');
  }
}

Review the updated code 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');
  }
}

Answer №2

rather than

document.querySelector("#schoolName").action = 'NHGLogin.php';


try this:

window.location.href = "NHGLogin.php";

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating seamless shading effects using three.js with Blender integration

When I import a Blender scene into a three.js environment, the curved faces appear flat. Is there a method to ensure that these surfaces remain smooth as intended? Despite calculating vertex normals and activating the smoothShaded option, the issue persis ...

having trouble compiling a react js file with webpack

One of my files, app.js, contains the following code snippet: handlePageClick = (data) => { let selected = data.selected; let offset = Math.ceil(selected * this.props.perPage); this.setState({offset: offset}, () => { this.setStat ...

What steps are necessary to create an npm package utilizing three.js without any dependencies?

I have a challenge - I am trying to create an npm package that generates procedural objects for three.js. The issue I'm facing is how to properly include three.js in my code. I attempted to establish a dependency and use something like const THREE = r ...

Separate the information into different sets in JavaScript when there are more than two elements

Upon extraction, I have obtained the following data: ╔════╦══════════════╦ ║ id ║ group_concat ║ ╠════╬══════════════╬ ║ 2 ║ a ║ ║ 3 ║ a,a ...

Tips for ensuring a div stays centered while resizing the window

When I resize the window, the div tab on the right side of the screen moves to the bottom right. How can I make it stay in the middle regardless of screen size? I've tried using margin-left:auto and margin-right:auto but it didn't work. Changing ...

What steps should I take to make the initiallyHidden attribute in FusionCharts work properly?

I am encountering an issue with my chart that is reloaded periodically. I want to make sure that the series hidden by the user (by clicking on their legend names) remain hidden even after reloading. I attempted to set the series initiallyHidden attribute t ...

HTML5 Local Storage allows web developers to store data locally within

After saving a value in the local storage (HTML5) on page 1, I noticed that when I navigate to page 2, the values stored in the local storage disappear. Any insights or suggestions on what might be causing this issue? Upon reviewing my code, I am using th ...

Learn how to hide elements on print pages conditionally in Vue.js using CSS or JavaScript

I am currently using the Vue framework to work on printing webpages for my application. I have an issue that needs a solution, which I will explain below. <template> <div id = "intro" style = "text-align:center;"> <div ...

Troubleshooting the AutoHeight Problem in Slidesjs

My issue revolves around Slidesjs which can be found at . Specifically, I am facing a problem with autoHeight feature not displaying properly in Chrome. Initially, the height of slides shows as 0px but upon clicking to the next slide, it adjusts to the c ...

Eliminate billing information from the Woocommerce order management page on the backend

Is there a way to modify the text "Paid on" in the backend order details page of WooCommerce? I have already implemented this for BACS and local pickup payment methods. Replace a specific word for BACS payment method in Woocommerce order edit pages I am ...

Is it possible to perform direct URL searches using react-router-dom?

Encountering an issue when attempting to directly copy a route, resulting in the following error: Error: Cannot Access / home Despite utilizing various methods such as browserHistory, I am unable to successfully render views when navigating with menu i ...

Distinguishing Font Sizes in Safari Compared to UIWebView

When the page is loaded in Safari, everything looks perfect. However, when viewed in a UIWebView, all the fonts appear enlarged. I've done my research and tried various solutions like: - Setting WebView.scalesPageToFit = true - Applying default con ...

Transfer the AWS configuration settings to the imported module

Currently, I am attempting to perform unit testing on a JS AWS Lambda by running it locally. To simulate the Lambda environment, I am taking on the same role that the Lambda would have with AWS.config.credentials and then executing the Lambda function that ...

Tips for passing variables through onclick to JavaScript file

My task involves querying a database and implementing a filter based on country selection from a map. After writing the JavaScript and PHP code, everything seems to work fine except for one issue - passing a name with an onclick function to initiate the qu ...

Delving into XFN data parsing using Jquery, part two

I need some assistance with correctly parsing the REL attribute on A tags. I have already figured out how to match most XFN values, but there are two that I'm struggling with: "co-worker" and "co-resident". The hyphen seems to be causing an issue with ...

Is there a copyright concern regarding CSS?

There are countless websites that spark my creativity. If I am inspired by a particular design, CSS, or JavaScript on one of these sites, am I allowed to copy it to my local folder and use it? For instance, let's say I come across a website called xy ...

The isotope plugin import failed, displaying the error message "The function $(container).isotope does not exist."

I've been struggling to get the Metafizzy Isotope plugin to work, but I'm having no success. The Network tab indicates that it's not loading properly. After installing isotope-layout and requiring it in my main-file.js, the code still doesn ...

Adjustable Scroll Bar

Could anyone help me find a component similar to the resizable scrollbar on Google Finance? Check out this link: http://www.google.com/finance?q=EURUSD&ei=bhM_UKDXF-aIWqAPVNQ You can see it below the chart. If you know where I can find something like ...

The most effective method for modifying a prop in VueJS without altering the parent's data

In the main Vue component, there is an object named user. If I pass this user object as a prop to a child component: <child :user="user"></child> When updating user.name in the child component, the changes also reflect in the parent componen ...

Creating a collection by gathering data from interactive fields with the help of AngularJS

I have a project to create an attendance system for employees. The system requires me to track the attendance status of each employee by generating a dynamic form with text input fields and checkboxes using angularjs ng-repeat inside a table. This form wil ...