How do I prevent the form from being redirected after submitting?

I searched the site and found various solutions, most of which suggested using

return false;

      

The problem is that I am using this piece of code to submit the form:

$(function() {  
    $("#snd").click(function() {  
        var dendar = "http://" + $("#inpt").val() + ":" + $("#pwd").val() + "secretsite.lol/something.xml";
        $("#formen").attr("action", dendar);
        $("#formen").submit();
        alert(dendar);
        return false;
    });  
});

      

A warning just to inform me that the form has been submitted.

Thanks guys!

+2


a source to share


4 answers


How about just using the POST AJAX call to the data handler? This looks like what you want.



+3


a source


You must return false in the submit () call event handler, not the click handler.

$("#formen").attr("action", dendar); $("#formen").submit(
   function() { 
      return false;
   }
);

      



As others have noted, this will stop the form submission. What you want to do is collect the form data and submit it using an Ajax call.

$. post (url, {var1: value1, var2: value2 etc ...});

+4


a source


You cannot prevent redirects, but you can submit the form to a (possibly hidden) iframe so that you don't leave the current page

Ajax will be better if the post is on the same server as the current page and you are not posting the file data

+4


a source


Using jquery 1.4:

$("#formen").live("submit",function(e) {  
   e.preventDefault();
}

      

This will prevent the actual form submission. Then you can do whatever should have been posted (use ajax, print something on the page ...).

+2


a source







All Articles