title: JavaScript function on onclick event of submit button not validating textbox
body: <p>I wrote this code (which posts to a PHP page) so that if guests do not input their name, it would pop out an alert box. However, Even when I put something in the name textbox and submit it, the alert box still pop out and it would submit the $_POST['gname'] to the server.</p>
  
  <p>Here’s the JavaScript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function gSubmit()
      {
          if (document.getElementById("gname").value.length === 0)
          {
            alert("For completing the form, it requires you to input a Name");
            return false;
          }
          else
          {
            document.getElementById("guestbook").innerHTML=("&lt;p&gt;&lt;b&gt;&lt;center&gt;Thank you for submitting the Guestbook");
            window.location = "guestbook.php";
            return true;
          }
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>Here’s the HTML:</p>
  
  <pre><code>&lt;form id="guestbook" name="guestbook" method="post" action="guestbook.php"&gt;
      &lt;center&gt;
          &lt;table border="0px"&gt;
              &lt;tr&gt;
                  &lt;td&gt;      
                      &lt;p align="right"&gt;Name :&lt;/p&gt;&lt;/td&gt;&lt;td&gt; &lt;input type="text" name="gname" id="gname" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;p align="right"&gt;E-mail :&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="gemail" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;p align="right"&gt;Telephone No :&lt;/p&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;input type="text" name="gtpn" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;p align="right"&gt;Product Order / Comment / Messages :&lt;/p&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;textarea name="gtxtfld" id="gtxtfld" cols="32"&gt;&lt;/textarea&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
  
      &lt;br /&gt;&lt;br /&gt;
  
      &lt;input type="submit" value="submit" name="submit" onclick="gSubmit();" /&gt;
      &lt;input type="reset" value="reset" name="reset" /&gt;
  &lt;/form&gt;
  </code></pre>