Can jquery.ajax post a value from an attribute Name? If can, I want send image’s title apple to b.php, then return the data This is an apple from b.php to the div#result, Thanks.
a.php
<html>
<head>
</head>
<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
<script language="javascript">
$(document).ready(function () {
var params = "value=" + $('#send').attr(title);
$.ajax({
url:'b.php',
type:'post',
dataType:'html',
data:params,
success:data
});
function data (html) {
$("#result").html(html);
}
});
</script>
<body>
<div id="result"></div>
<img id="send" src="apple.jpg" title="apple" />
</body>
</html>
b.php
<?php
echo 'This is an '.$_REQUEST['value'].'.';
?>
If you put quotes around the attribute name,
.attr('title'), it should work. Otherwise,titlewill be treated as variable, and, as it is not defined, it will have the valueundefined.But better is to pass an object to
dataand let jQuery take care of proper encoding: