jquery ajax代码不起作用

时间:2012-12-07 10:48:25

标签: php javascript jquery html ajax

我在jquery和ajax中编写了一个代码作为研究的例子。但它没有用。这是代码。

jquery的

$(document).ready(function() {    
    $("#ra").click(function(){  
        var value=145;
        $.ajax({
            url: "ajax.php",
            type: "POST",
            data: ({name: value}),
            success: function(data){
            $("#raaagh").html(data);
            }
        });        
    });
});

PHP

<?php
    $score = "1";    
    $userAnswer = $_POST['name'];    
    if ($_POST['name'] == "145"){
        $score++;
    }       
    echo $score;    
?>

HTML

<button id="ra">Ajax Away</button>
<div id="raaagh"></div>

2 个答案:

答案 0 :(得分:2)

这是工作代码..我想你错过了包括JQuery

html.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
    $("#ra").click(function(){  
        var value=145;
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: ({name: value}), //you can POST multiple parameters
            //data: ({name: value, email:value, phone: value}),
            success: function(data){
                $("#raaagh").html(data);
            }
        });        
    });
});
</script>
</head>
<body>
    <button id="ra">Ajax Away</button>
    <div id="raaagh"></div>
</body>
</html>

ajax.php:

<?php
    $score = 1;
    $userAnswer = $_POST['name'];    
    if ($_POST['name'] == "145"){
        $score++;
    }       
    echo $score;    
?>

答案 1 :(得分:-1)

试试这个:

echo json_encode($ score);