将 jQuery 变量传递给 PHP Symfony2
Pass jQuery variable to PHP Symfony2
我正在尝试使用 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
的数据库中进行评级
在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();
}
it alert star 传递给服务器
我正在尝试使用 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
在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();
}
it alert star 传递给服务器