How to get to a page with a successful form?ю

<form action="sub.html" method="POST" name="fo">
  Ваше имя: <input type="text" id="f1" maxlength="30"><br><br>
  E-mail: <input type="text" id="f2" maxlength="30"><br><br>
  <input type="submit" id="bu" value="Подписаться на новости">

</form>
document.forms["fo"].onsubmit = test;
function test() {
  if (document.getElementById("f1").value.length < 2) {
    document.getElementById("bu").value = "Вы не ввели Имя";
    document.getElementById("bu").style.color = "red";
    return false;
  } else {
    if (document.getElementById("f2").value.length < 8) {
      document.getElementById("bu").value = "Вы не ввели E-mail";
      document.getElementById("bu").style.color = "blue";
      return false;
    } else {
      return true;
    }
  }
 
}

does not display content more accurately, what does document.forms[“fo”].onsubmit = test; mean?

document.forms is a collection of all the tags in the page. You can access a particular form either by it’s index position, or it’s name. This code is access the form by name using ["fo"].

.onsubmit is an older way of attaching code to the submit event for the form. HTML elements all have an on* attribute that corresponds to each available event. Assigning a function to these properties will run the function whenever the event occurs.

So, the whole line document.forms["fo"].onsubmit = test; is assigning the test function to the submit event of the form element with name="fo".

Why is the additional page not showing?

What additional page? sub.html? If the test function returns false then it will cancel the form submission, which cancels loading sub.html.

I specified true if the fields are filled in correctly, but nothing works

Then you need to elaborate more about that. Saying “it doesn’t work” without any other details is not helpful. Did you check your browser’s developer console for errors? Does it show any? If so, what are they. Describe what actually happens vs what you expect to happen, in detail.

The code posted works just fine. If your tests fail, the form isn’t submitted. If they pass, it is submitted (giving a 404 since sub.html doesn’t exist on that server).

According to the flow of your provided code, Your input fields are missing the name attribute, which is typically used to identify form data that gets sent to the server. You might want to add them if you intend to process this data server-side.

That’s why the form is not working.

I needed the sub page to open, but after I changed the post to get, everything worked.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.