我无法使以下AJAX请求正常工作:
我想让在字段中输入的文字显示在" p"标签
服务器:Apache 2.2 PHP 5
我的HTML:
<html>
<head>
<title>Test for reponder</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$function send() {
$('body').on('click', 'button', function(event) {
var namee = document.getElementById('name').value;
//var dataString='name ='+ name;
$.ajax({
type: 'POST',
url: 'responder.php',
data: namee,
success: function(html) {
$('#msg').html(html);
}
})
</script>
</head>
<body>
<input type="text" id="name">
<!--<input type="submit" id="button" value="Send" onclick="return send();">-->
<button type="button">Send</button>
<p id="msg"></p>
</body>
</html>
&#13;
我的PHP文件:
<?php
$name=$_POST[ 'name'];
echo "response:" . $name;
?>
&#13;
答案 0 :(得分:0)
您可以这样做:
<强> HTML 强>
<input type="text" id="name">
<button type="button">Send</button>
<p id="msg"></p>
<强>的jQuery 强>
<script>
$(document).ready(function(){
$(document).on('click', 'button', function(){
var name = $("#name").val();
var param = {name: name};
$.ajax({
type: 'POST',
url: 'responder.php',
cache: 'false',
data: param,
beforeSend: function(){
// before send
},
success: function(data){
// success
$('#msg').html(data);
},
error: function(){
// error
}
});
});
});
</script>
<强> responder.php 强>
<?php
if(isset($_POST['name'])){
$name=$_POST['name'];
echo "response:" . $name;
}
?>