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="su开发者_Python百科bmitThisID"></span>
$('#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.
#submitThisID.post(
'http://example.com/',
{
'foo': 'bar',
},
function (data) {
console.log( 'data: ' + data );
}
);
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?
Yes, have a read of this: http://api.jquery.com/jQuery.post/
$('#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!');
}
});
});
精彩评论