views:

118

answers:

3

How can we send a request using the HTTP POST method via javascript...without submitting a form?

+8  A: 

By using AJAX.

Darin Dimitrov
+10  A: 

I prefer to use jQuery.post() - for example:

$.post("test.php", function(data){
  alert("Data Loaded: " + data);
});
Justin Ethier
Who doesn't :-)
Darin Dimitrov
That does require adding jQuery to your system (although if you are going to use AJAX it is much easier if you use some library package)
Kathy Van Stone
Absolutely, but at this point anyone working with JavaScript needs a good reason for NOT using a library such as jQuery.
Justin Ethier
Hmmm.. the fact that my Javascript code is almost always far smaller than the massive 24kb (not including plugins) I need to load to use jQuery would be a fairly good reason imho.
lucideer
A: 
<form id="myform">
<input type="text" name="weather" value="sunny" />
</form>

<a href='javascript:submit_form()'>Click here to submit the form</a>

<script language='javascript'>
function submit_form()
{
   var myform = document.getElementById('myform');
   myform.submit()
}
</script>
Gregory Belton
Sorry just read you put without submitting the form!! You can use Prototype.js, it has a form.serialize method. You can then use a Prototype Ajax call to send the data to the server without submitting the page.
Gregory Belton