将jQuery变量传递给PHP Symfony2

时间:2015-08-05 09:52:41

标签: php jquery ajax symfony rating

我正在尝试使用Symfony2进行星级评分教程

rating.css

fieldset, label { margin: 0; padding: 0; }
body{ margin: 20px; }
h1 { font-size: 1.5em; margin: 10px; }

.rating {
    border: none;
    float: left;
}

.rating > input { display: none; }
.rating > label:before {
    margin: 5px;
    font-size: 1.25em;
    font-family: FontAwesome;
    display: inline-block;
    content: "\f005";
}

.rating > .half:before {
    content: "\f089";
    position: absolute;
}

.rating > label {
    color: #ddd;
    float: right;
}

.rating > input:checked ~ label,
.rating:not(:checked) > label:hover,
.rating:not(:checked) > label:hover ~ label { color: #FFD700;  }

.rating > input:checked + label:hover,
.rating > input:checked ~ label:hover,
.rating > label:hover ~ input:checked ~ label,
.rating > input:checked ~ label:hover ~ label { color: #FFED85;  }

在视图中test.html.twig

<html>
<head>
    <link rel="stylesheet" href="{{ asset('bundles/hearwegohearwego/css/rating.css') }}" />
    <script src="{{ asset('bundles/hearwegohearwego/js/jquery.min.js') }}"></script>
    <script>
        $(function () {
            $("#demo1 .stars").click(function () {
                var id=$(this).attr('id');
                $.ajax({
                    url: "{{ path('test') }}",
                    type: 'POST',
                    data: {id1:id},
                    success: function(result)
                    {
                        alert(id);
                    }
                });
            });
        });
    </script>
</head>
<body>
<fieldset id='demo1' class="rating">
    <input class="stars" type="radio" id="star5" name="rating" value="5" />
    <label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input class="stars" type="radio" id="star4" name="rating" value="4" />
    <label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input class="stars" type="radio" id="star3" name="rating" value="3" />
    <label class = "full" for="star3" title="Meh - 3 stars"></label>
    <input class="stars" type="radio" id="star2" name="rating" value="2" />
    <label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
    <input class="stars" type="radio" id="star1" name="rating" value="1" />
    <label class = "full" for="star1" title="Sucks big time - 1 star"></label>
</fieldset>
</body>
</html>

DefaultController.php中(在test.html.twig脚本中声明的右侧文件夹中):

/**
     * @Route("/test", name="test")
     */
    public function testAction(Request $request)
    {
        if($request->isXmlHttpRequest()) {
            $b=$request->get('id1');
            echo $b;
        }
        return $this->render('HearWeGoHearWeGoBundle::test.html.twig');
    }

当我点击星标时,它会在jQuery脚本中发出包含变量id的消息警告,但没有任何内容按我的意愿回显。请帮我解决这个问题,之后,我将使用本教程在我的数据库中对Symfony2进行评级

1 个答案:

答案 0 :(得分:1)

在javascript更改成功功能中:

                success: function (result)
                {
                    alert(result)
                    console.log(result)
                }

并在控制器中:

public function testAction(Request $request)
    {
//        print_r($request);
        if($request->isXmlHttpRequest()) {
            $b=$request->request->get('id1');
            echo $b;
        }
        return new \Symfony\Component\HttpFoundation\Response();
    }

警告明星传递给服务器