blue-sky
blue-sky

Reputation: 53806

jQuery form submit

When I submit below form the current page is refreshed and form parameters are appended to the end of the page URL. Why is this occuring ? I need to submit the form but the keep the form parameters hidden.

Thanks

<div class="ui-block-b"><button id="submit" type="submit">Submit</button></div>

            $("#submit").click(function(){

                var formData = $("#newPostForm").serialize();

                $.ajax({
                    type: "POST",
                    url: "/mobile/newpost.php",
                    cache: false,
                    data: formData,
                    success: onSuccess
                });

                return false;
            });

Upvotes: 1

Views: 434

Answers (1)

onteria_
onteria_

Reputation: 70487

Get rid of the type="submit" (which by the why should actually be <input type="submit">) and just make it a standard button if you don't plan on actually submitting the form:

<input type="button" id="submit" value="Submit" />

An alternative method would be to set an onsubmit handler on the form:

$(document).ready(function() {
  $("#newPostForm").submit(function(){

    var formData = $("#newPostForm").serialize();

    $.ajax({
      type: "POST",
      url: "/mobile/newpost.php",
      cache: false,
      data: formData,
      success: function(data) {
         window.location.href = "/where/to/go";
      }
    });

    return false;
  });
});

Upvotes: 1

Related Questions