views:

208

answers:

4

I have searched the site and I've found various solutions, most of them suggesting using

return false;

Problem is that I'm 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;
    });  
});

The alert is just there to let me know that the form has been submitted..

Thanks guys!

+3  A: 

You must return false within the submit() call's event handler, not the click handler.

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

As pointed out by others, this will stop the form from submitting at all. What you want to do is collect the form data and submit it using an Ajax-call.

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

thomask
Hey, thanks for the answer!Either I'm dumb and don't know how to pull this of or it's not working with my code.This disabled my submittion entirely, I'm not getting the forms posted, and the alert isn't showing up either (I put in the alert before the return statement)..
Noor
But that's exactly what it does, it disables submission entirely. Apparently you're not asking the right question.
drachenstern
Sorry.. I need to my form to send information to a site, without having to redirect. When I click on the submit button, I need it to do that, post the info and just alert me that it's done that..
Noor
You should really check out the jQuery Ajax functionality.
thomask
+3  A: 

What about just using an AJAX call with POST to a data handler? That sounds like what you want.

drachenstern
I'm not sure but AJAX only works so long the data im sending is to the same server I'm posting from? If so then thats not an option..
Noor
Why would you be submitting cross server? It's possible to do nearly anything from a browser window with javascript, just depends on what you're attempting to do. Remember that ad services load ads from client networks. You don't think that the server retrieves all those ads and injects them into the page do you?
drachenstern
Anything you can do with plain HTML can be done with javascript. If you can submit now to the other server, then you can jquery.ajax post it over to that server as well.
drachenstern
Okay, thanks for enlightening me! I learn something new everyday thanks to you guys
Noor
have you seen anything like this post (since there are so many reposts of everyone everywhere on the internet) before? http://jquery-howto.blogspot.com/2009/04/jquery-twitter-api-plugin.html
drachenstern
+2  A: 

You can't prevent a redirect, but you can submit the form into a (possibly hidden) iframe so you don't leave the current page

Ajax would be better if the post is to the same server as the current page and you aren't posting file data

Jason Harwig
I'll try to do that, the post is made to a website that doesn't belong to me.. Thanks!
Noor
+1  A: 

Using jquery 1.4:

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

This will prevent the form from actually submitting. Then you're free to do whatever with the data that was supposed to be posted (use ajax, print something on the page...).

marcgg
Not quite sure where to put this in, does this replace the submit codelines?
Noor
@noor add it where you want, just make sure it's called (on document ready for instance)
marcgg