0
<html>

<head>
  <title> Newsletter Application Form</title>
  <script>
    function validate() {
      if (document.getElementById("txtName").value == "") {
        alert("Name must be filled out");
      }
      //else if (email == "") {
      //invalid();
      //}
    }
  </script>
</head>

<body>
  <form name="frmNews" onsubmit="validate()">
    <h2>Newsletter Application</h2>
    <p>To get our newsletters in your inbox, give us a few details
      about yourself.<br />Then tell us why you are interested in this topic in
      100 words or less.</p>
    <table>
      <tr>
        <td class="frmDetails">Name:</td>
        <td><input type="text" id="txtName" size="20" /></td>
      </tr>
      <tr>
        <td class="frmDetails">Email:</td>
        <td><input type="text" id="txtEmail" size="20" /></td>
      </tr>
      <tr>
        <td class="frmDetails">Topic:</td>
        <td>
          <input type="radio" id="radNewsletter" value="" />Health and Wellness<br />
          <input type="radio" id="radNewsletter" value="" />Creative Writing<br />
          <input type="radio" id="radNewsletter" value="" />Gardening
          <br />
        </td>
      </tr>
      <tr>
        <td class="frmDetails">I am interested in this topic because:</td>
        <td>
          <textarea id="txtTopic" cols="40" rows="5" />
          </textarea>
        </td>
      </tr>
      <tr>
        <td class="frmDetails"></td>
        <td><input type="submit" value="Submit" /></td>
      </tr>
    </table>
  </form>
  <p id="isValid"> </p>
</body>

</html>

For some reason, when I click submit, it flashes what it should onto the screen for a brief second, then it brings me to a 404 page. The problem is, my code shouldn't send me anywhere, so I have no idea what could be the issue. Any help?

I tried commenting out as much as I can, but it still brings me to the 404 when it shouldn't. I want this to not send me to a 404 while doing what it does right now.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
R3Ked
  • 1
  • 2
    Why shouldn't it send you anywhere? You're submitting a form. – Dave Newton Jan 14 '23 at 19:07
  • *"my code shouldn't send me anywhere"* - Are you just asking how to cancel a form submission? – David Jan 14 '23 at 19:07
  • 1
    You should use `onsubmit="validate(event)"`. Then the function should accept an `event` parameter, and call `event.preventDefault()` to cancel the form submission. – Barmar Jan 14 '23 at 19:09
  • Please see [How to prevent form from being submitted?](/q/3350247/4642212). Use `document.querySelector("[name=frmNews]").addEventListener("submit", (event) => { event.preventDefault(); /* Perform validation. When ready to submit: */ event.target.submit(); });`. Inline event handlers like `onsubmit` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Jan 14 '23 at 19:09
  • @SebastianSimon I'm not talking about global event, this is the local `event` variable in the `onXXX` code. – Barmar Jan 14 '23 at 19:20
  • @Barmar Cool, I didn’t know `event` was an argument according to [step 9 when evaluating `on*` attribute source text](//html.spec.whatwg.org/multipage/webappapis.html#getting-the-current-value-of-the-event-handler). – Sebastian Simon Jan 14 '23 at 19:34
  • my form wasn't supposed to do anything yet lol, will try this is for school btw – R3Ked Jan 14 '23 at 21:01

0 Answers0