Using POST

[js]
function DoAction( id, name )
{
$.ajax({
type: "POST",
url: "someurl.php",
data: "id=" + id + "&name=" + name,
success: function(msg){
alert( "Data Saved: " + msg );
}
});
}
[/js]

Using GET

[js]
function DoAction( id, name )
{
$.ajax({
type: "GET",
url: "someurl.php",
data: "id=" + id + "&name=" + name,
success: function(msg){
alert( "Data Saved: " + msg );
}
});
}
[/js]

A, perhaps, better way to do this that would work (using GET) if javascript were not enabled would be to generate the URL for the href, then use a click handler to call that URL via ajax instead.
[js]
<a class="ajax-link" href="/someurl.php?id=1&amp;name=Jose"> Click </a>
<a class="ajax-link" href="/someurl.php?id=2&amp;name=Juan"> Click </a>
<a class="ajax-link" href="/someurl.php?id=3&amp;name=Pedro"> Click </a>
<a class="ajax-link" href="/someurl.php?id=n&amp;name=xxx"> Click </a>
[/js]

[js]
<script type="text/javascript">
$(function() {
$(‘.ajax-link’).click( function() {
$.get( $(this).attr(‘href’), function(msg) {
alert( "Data Saved: " + msg );
});
return false; // don’t follow the link!
});
});
</script>
[/js]

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation