0

我写的这个 jquery 代码有问题:

$('.star').click(function(){

        $('#errors').removeClass().html('');
        var term = $(this).id;
        var posting = $.get("aj_vote_opere.php", { rating: term, opera: '<?php echo $_GET['id']; ?>' },
        function(data) {
        $('#errors').addClass(data.stileError).html(data.message);          
    }, "json");
}

我的 HTML 代码如下所示:

<div class="rating">
<span id="5" class="star">&star;</span><span id="4" class="star">&star;</span><span id="3" class="star">&star;</span><span id="2" class="star">&star;</span><span id="1" class="star">&star;</span>
</div>

<div id="errors"></div>

在 php 文件中,我想将投票存储在数据库中。似乎没有发送 GET var。我想是因为我没有要提交的表格。我以前使用过 $.post 但我意识到没有表格 = 没有帖子。但是 $.get 呢?我真的很感激任何提示。提前致谢。

编辑

这是我的 aj_vore_opere.php:

<?php
include('Connections/dbConn.php');

    if(!isset($_SESSION['u_id']))
    {
        $voto_q = "INSERT INTO ar_opere_rating (rate,opera) VALUES ('".$_GET['rating']."','".$_GET['id']."')";
        $voto_x = fln_query($voto_q);

        $msg = $voto_q.'Il tuo voto &egrave; stato correttamente registrato.';
        $bgClass = ' ok';
    }
    else
    {
        $msg = 'Attenzione, devi essere registrato per votare. Vuoi registrarti? <a href="reg.php">Clicca qui</a>';
        $bgClass = ' ko';
    }


    echo json_encode(
        array(
        'message'=>$msg,
        'stileError'=>$bgClass
        )
    );
?>
4

2 回答 2

0

这行得通。
我还在JSFiddle上对此进行了测试。还要检查您发送到服务器的数据 { rating: term, opera: '<?php echo $_GET['id']; ?>' }。在您的 JS 中,您将对象键设置为歌剧,并且您试图通过$_GET['id']在 PHP 中访问它。

$('.star').click(function(){
    var term = $(this).attr('id');
    $('#errors').removeClass().html('');

    $.ajax({
        url: 'path/to/your/server',
        type: 'post',       
        dataType: 'json',
        data: { rating: term, opera: "<?php echo $_GET['id']; ?>"},
        success: function(data) {
            console.log(data);
            $('#errors')
            .addClass(data.stileError)
            .html(data.message);          
        },
        error: function(xhr, errmsg) {
             console.log(errmsg);   
        }
    });

});
于 2013-06-23T10:33:13.540 回答
0

试试这个也许?

   $('.star').click(function(){

    $('#errors').removeClass().html('');
    var term = $(this).attr('id');
    $.ajax({
         url:"aj_vote_opere.php", 
         data: { rating: term, id: <?php echo $_GET['id']; ?>},
         dataType: "jsonp",
         success:function(data){
         console.log(data);//for debugging...
         alert(data.message);//for debugging...
         $('#errors').addClass(data.stileError).html(data.message);  
         }
         })
    });
于 2013-06-22T18:10:42.840 回答