0

I am trying basic database insert using ajax and inserting part works fine the problem is that page is getting redirected after inserting completed.I had came acroos this issue before and found a solution (changed the src part like below)

src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

But that solution didt work this time what am i dıing wrong again?

this is index.php

 <!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
 <script type="text/javascript" src="site.js"></script>
</head>
<body>
<form action="process.php" id="myForm"method="post">
 <input type="text" name="uname"><br>
 <input type="text" name="pass"><br>
 <input type="text" name="fname"><br>
 <input type="text" name="lname"><br>
 <button id="submit" value="Insert"/> <br>
</form>
<div id="ack"></div>
</body>
</html>

and this is my script

 $("#submit").click( function() {
 $.post( $("#myForm").attr("action"),
 $("#myForm :input").serializeArray(),
 function(info) {
 $("#ack").empty();
 $("#ack").html(info);
 clear();
 });
});
$("#myForm").submit( function(event) {
 event.preventDefault();
 return false;
});
function clear() {
 $("#myForm :input").each( function() {
 $(this).val("");
 });
}
asked Sep 30, 2016 at 19:43
1
  • You can add a type="button"attribute to your button so that it will not act as a submit button (the default type). Commented Sep 30, 2016 at 19:55

4 Answers 4

1

The submit call doesn't really belong in the click handler, move it out and use event.preventDefault() if you want to avoid form submission by means other than the click handler you created.

$("#submit").click( function() {
 $.post( $("#myForm").attr("action"),
 $("#myForm :input").serializeArray(),
 function(info) {
 $("#ack").empty();
 $("#ack").html(info);
 clear();
 });
});
$("#myForm").submit( function(event) {
 event.preventDefault();
 return false;
});
function clear() {
 $("#myForm :input").each( function() {
 $(this).val("");
 });
}
answered Sep 30, 2016 at 19:56
Sign up to request clarification or add additional context in comments.

4 Comments

if the data from the fields of form is send to insert via $.post method so why there is a need of submitting the form via submit function separately ?
There isn't a need. $('#myForm').submit(...) is not strictly necessary, but may serve as a fallback to prevent a situation where the browser submits the form by some means other than the user clicking the button (e.g. pressing enter in a text field).
The other option would be to move the $.post(...) into the submit handler (after the event.preventDefault()), and either have the button click trigger the submit, or change the button into a standard <input type="submit">.
@Mark H. thank you for your suggestion I have updated the script as you suggested but still getting redireted
0

Switch to a .submit() event handler:

$("#myForm").submit(function(event) {
 $.post($("#myForm").attr("action"),
 $("#myForm :input").serializeArray(),
 function(info) {
 $("#ack").empty();
 $("#ack").html(info);
 clear();
 });
 event.preventDefault();
});
answered Sep 30, 2016 at 20:25

Comments

0

Description

  1. Wrap your $.post code in submit handler function.
  2. Use event.preventDefault() function, to prevent the form submission.
  3. Use $('#myform')[0].reset() for clearing the input fields instead of using the separate clear function.

Code

$("#myForm").submit(function (event) {
 $.post($("#myForm").attr("action"),
 $("#myForm :input").serializeArray(),
 function (info) {
 $("#ack").empty();
 $("#ack").html(info);
 // instead of using the clear() function
 // separately you may clear all the empty 
 // fields like this
 $('#myform')[0].reset(); 
 });
 event.preventDefault();
});
answered Sep 30, 2016 at 21:11

Comments

0

Not sure if this will work with jQuery 1.8.2 but here is how I've accomplished this same thing in the past.

$('#myForm').on('submit', function(e) {
 e.preventDefault();
 var details = $('#myForm').serialize();
 $.post('process.php', details, function(data) {
 $('#ack').html(data);
 $('#myForm').trigger("reset");
 });
});
answered Sep 30, 2016 at 21:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.