Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I've been trying to figure out the method of posting form data from a textarea using jQuery's ajax() function to a mysql database. Problem is, I don't really understand the theory of doing so.

Say there's a form:

<form method="post" action="action.php">
  <textarea name="somecontent" rows="5" cols="30">Some content</textarea>
  <input type="submit" name="submit" value="Post to db using ajax" />
</form>

The form points to action.php which processes data, yadayada. In a theoretical sense, how could I manipulate jQuery ajax to post the data rather than directly submitting the form data to action.php?

Edit: I don't understand how to send the data with ajax.

share|improve this question
Define "directly submitting". – AsTheWormTurns Dec 1 '11 at 15:58
1  
You mean you want it to update in realtime while typing, or when the button is clicked? Or is there another reason you need Ajax for? – Nebojsac Dec 1 '11 at 16:02
What I mean to say is what would using ajax to send the data on submit look like and how does that work? That's where I'm lost. – Dilwin Dec 1 '11 at 16:17

1 Answer

up vote 1 down vote accepted

You need to catch the form submit event in javascript, prevent the default action (submitting...) and call your ajax / jQuery submit instead.

$("form").submit(function(){
  // do your stuff
  $.post(
    "action.php",
    // add all stuff, see their page
  );

  return false; // prevent the original form action from happening
});

See jQuery's $.post or $.ajax for more information.

share|improve this answer
This I already know, I'm just not understanding how to use ajax to pass the data to the server. – Dilwin Dec 1 '11 at 16:24
1  
@Dilwin This is how I read your question, but I'll add a jQuery ajax reference... – jeroen Dec 1 '11 at 16:28
Thank you, I appreciate it :) – Dilwin Dec 1 '11 at 16:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.