dtv995719 2015-08-05 09:52
浏览 44
已采纳

将jQuery变量传递给PHP Symfony2

I'm trying to do a star rating tutorial with Symfony2

In 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;  }

In View 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>

In DefaultController.php (in right folder declared in test.html.twig script):

/**
     * @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');
    }

When I click on stars, it does alert with a message contaning variable id in jQuery script, but nothing is echoed as I want. Please help me solve this problem, after that, I will use this tutorial for rating in my database in Symfony2

  • 写回答

1条回答 默认 最新

  • dourao1968 2015-08-05 11:32
    关注

    In javascript change success function:

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

    and in controller:

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

    it alert star passed to server

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题