Reputation: 12512
Is it possible to submit with POST method an ID when an element is clicked? I think jQuery has a built in AJAX fanction...
<span id="submitThisID"></span>
Upvotes: 1
Views: 1070
Reputation: 5211
$('#submitThisId').click(function() {
$.post("postTo.php", { id: $(this).attr('id') }, function(response) {
alert(response);
});
});
This will post your id to postTo.php and can be retrieved using: $id = $_POST['id'];
Whatever the postTo.php file returns will be alerted to the screen (response).
EDIT: To submit to the same page and do some server-side actions (db query, etc) you would only need to put something similar to the following code at the top of your page:
<?php
if (isset($_POST['id'])) {
$id = $_POST['id'];
// Perform some db queries, etc here
$response = 'Format a response here'; // Format a response
// This will return your formatted response to the $.post() call in jQuery
return print_r($response);
}
?>
When that "response" (text, html, etc) is returned to jQuery, you can use simple jQuery to insert it into the page where necessary and change the look of the page.
Upvotes: 2
Reputation: 359986
It's definitely possible: $.post()
.
However, your question doesn't make sense. Submitted values in HTTP posts take on name/value pairs. An example POST body might look like this:
Name: Jonathan Doe
Age: 23
Would the element ID be name, or the value? What would the other one be?
Upvotes: 1
Reputation: 74272
#submitThisID.post(
'http://example.com/',
{
'foo': 'bar',
},
function (data) {
console.log( 'data: ' + data );
}
);
Upvotes: 1
Reputation: 18139
$('#submitThisID').click(function({
var id = $(this).attr('id');
$.ajax({
url:'/index.php/ajax/',
type:'post',
dataType:'json',
data:{
id:id;
},
success:function(data){
console.log('ajax success!');
}
});
});
Upvotes: 0