-4

我在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>
4

2 回答 2

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;    
?>
于 2012-12-07T11:14:25.840 回答
-1

尝试这个:

回声 json_encode($score);

于 2012-12-07T11:00:51.283 回答