Pass jQuery variable to PHP Symfony2 -
i'm trying star rating tutorial 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 - 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 click on stars, alert message contaning variable id
in jquery script, nothing echoed want. please me solve problem, after that, use tutorial rating in database in symfony2
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 server
Comments
Post a Comment