tags:

views:

39

answers:

1

Hi, I am trying to make a Ajax form Submission in Jquery. The form itself is loaded via an Ajax Event

The Form Element looks like this

<form id="create_user form" action="http://127.0.0.1/create_user" method="post">

  <p>Name
  <input type="text" name="name" value="" id="name"></p>
  <p>Employee Number:<br>
  <input type="text" name="emp_no" value="" id="emp_no"></p>
  <p>Email:<br>
  <input type="text" name="email" value="" id="email"></p>

  <p><input type="submit" name="submit" value="Save"></p>

  </form>

When loaded as html it works fine.

However I am trying to load it in another page via Ajax event

So the result looks like this

<div class="admin_main" id="admin_main">
<form id="create_user form" action="" method="post">

   .....Form......
<p><input type="submit" name="submit" value="Save"></p>
</form>

</div>

The Jquery code inside document ready function is

$("#admin_main").delegate("#create_user_form", "submit" ,function() {
$.post('http://127.0.0.1/create_user',  $("#create_user_form").serialize(), function(data){
                $("#admin_main").html(data);
            }, "html");
});

However this has the effect of merely reloading the page. I have been at it for hours. Where am I going wrong?

A: 

Hi,

I think you forgot the return: false; (or event.preventDefault() / event.stopPropagation() - not sure what is needed here) in the submit function. You definitely must stop the submit event somehow.

Alin Purcaru
return: false; and event.preventDefault()>>>> tried both. Only the page reloads.
RisingSun
Hi, thanks for the answer. That was indeed the problem. function(e){.....} e.preventDefault();
RisingSun