Jquery使用php进行ajax调用例子

By | 2010/04/10

使用Jaquery结合php可以很方便实现ajax调用。 下面是一个例子:

HTML代码:

—————————————————————————

<html>
<head>
<title> TEST </title>
<meta name=”generator” content=”editplus” />
<meta name=”author” content=”” />
<meta name=”keywords” content=”” />
<meta name=”description” content=”” />
<script type=”text/javascript” src=”jquery-1.2.6.pack.js”></script>
<script type=”text/javascript”>
<!–
$(document).ready( function () {
$(‘#btn’).click ( function () {
$.ajax({
url: ‘http://localhost/call.php’,
type: ‘POST’,
data:’name=’+$(‘#name’).val(),
dataType: ‘html’,
timeout: 1000,
error: function(){
alert(‘Error loading XML document’);
},
success: function(html){
$(‘#text’).html(html);
}
});
});
})
//–>
</script>
</head><body>
<div id=”text”></div>
<input type=”text” id=”name” name=”name” />
<input type=”button” value=”DO IT .” id=”btn”/>
</body>
</html>

—————————————————————————

call.php代码

—————————————————————————

<?php
$name = $_POST[‘name’];
echo “you say : “.$name;

?>

—————————————————————————