title: Submit ajax form and stay on same page not working
body: <p>I want to store comments from users in my databse. When a user submits I don't want to redirect them to a new page.
  I have the following code but it's not working.</p>
  
  <p>My HTML code:</p>
  
  <pre><code>&lt;form id="formA" action="test.php" method="post" enctype="multipart/form-data"&gt;
  &lt;input id="commentData" name="commentData" type="text" &gt;'
  &lt;input type="submit" value="toDb" id="toDB" name="toDB" /&gt;&lt;/form&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>var frm = $('#formA');
  
  $(document).submit(function(e) {
  e.preventDefault();
  
  $.ajax({
  
      url: frm.attr('action'),
  
      type: frm.attr('method'),
  
      data: frm.serialize(),
  
      success: function(html) {
  
          alert('ok');
  
      }
  
  });
  });
  </code></pre>
  
  <p>Here is my PHP file:</p>
  
  <pre><code>//Connect to database server
  mysql_connect("localhost", "user", "") or die (mysql_error ());
  mysql_select_db("test") or die(mysql_error());
  $strSQL = "SELECT * FROM comments order by RAND() LIMIT 5";
  $rs = mysql_query($strSQL);
  
  if (!$rs) {
       echo 'Could not run query ' . mysql_error();
       exit;
  }
  
  $dt1=date("Y-m-d");
  
  if(isset($_POST['toDB'])){
    $dataA = $_POST['commentData'];
    $sql = "INSERT INTO comments(id, comment, datum)VALUES(DEFAULT,'$dataA', '$dt1')";
    $result=mysql_query($sql);
  }
  mysql_close();
  </code></pre>
  
  <p>When I click on the submit button it will stay on the same page and show the alert but the data of the input field is not inserted in my database. When I remove the e.preventDefault() the data goes into the database but the page redirects to test.php</p>
  
  <p>Tried different things but can't figure it out.
  Can someone help me out?
  Thanks in advance!</p>