tags:

views:

37

answers:

3
A: 

To submit a form using jQuery I believe you need to check this out: http://api.jquery.com/submit/

And I am sure you can retrieve the submitted data with the following:

$('[name=mytextarea]').val()`
JamesStuddart
.val() has issues with some older browsers. Its best to use .attr('value') for compatibility.
Scott
+1  A: 

Yes, sending forms with jQuery is fairly easy.

There's a simple function to post data with AJAX, and it's easy to tie in the values of a form:

$.post(url, $('#myform').serialize(), function(data) { 
    $('#content').prepend(data);
})

$.post is the simple way. If you need to check for errors and whatnot, you should check out $.ajax (http://api.jquery.com/jQuery.ajax).

pianohacker
A: 

Yes, this is certainly possible, using AJAX:

$('.myform').submit(function(e){
    var data = $(this).serialize();
    e.preventDefault();

    $.ajax({
        url: this.action,
        type: 'POST',
        success: function(data) {
            $('#content').prepend(data);
        },
        error: function() {
            alert('oops, something went wrong');
        }
    );
});
lonesomeday
If I have various forms with the same class, how I can use only the one that user has click "Send"?
Francesc
I've just noticed that your question gave the form a `class` of `myform`, rather than an `id`. I have updated the answer accordingly. This action will only occur on the form that the user submits. The `submit` function means "when one of these forms is submitted, do this instead".
lonesomeday
When I click on the "Send" button, the browser loads the `action` URL instead of sending in the same page. Do you know what's happening?
Francesc
Yes, you probably didn't copy the `e.preventDefault()` line from the code above. This would be because I forgot to include it the first time and had to edit the code to correct this! Apologies.
lonesomeday
I copied it. Check it http://jsfiddle.net/kBzNC/2/ The browser refreshes to the action URL and that's all.
Francesc
I'm not sure what wasn't working, but this seems to work: http://jsfiddle.net/lonesomeday/HaR87/1/ You need to use `type: 'POST'` rather than `method`, sorry. You'll need to remove the `data+=` line for your script.
lonesomeday
I couldn't use this script. The browser redirects to the url specified in the `action`. I don't know what's happening.
Francesc