jquery得到没有提交表格

时间:2013-06-22 17:55:48

标签: jquery ajax post get

我遇到的问题是我写的这个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文件中,我想将投票存储在DB中。 似乎没有发送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
        )
    );
?>

2 个答案:

答案 0 :(得分: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);  
         }
         })
    });

答案 1 :(得分:0)

这很有效。我也在JSFiddle上对此进行了测试。 还要检查要发送到服务器的数据 { rating: term, opera: '<?php echo $_GET['id']; ?>' }。 在您的JS中,您将对象键设置为 opera ,并且您尝试通过 $ _ 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);   
        }
    });

});