我问你,因为我遇到了问题(我之前搜索过)。
我想从输入字段更新数据库,而无需用户注意。
我的代码:
<script>
function updateResult(str, idg, idt) {
if (str.length == 0)
return;
else
// alert("data : " + str + "; id_game : " + idg + "; id_team : " + idt);
$.ajax({
url: Routing.generate('basket_admin_tournament_update', { "game_id": idg, "team_id": idt, "score": str}),
type: "POST"
data: { game_id : idg, team_id: idt, score: str },
success: function(data) {
alert('ok');
},
error: function() {
alert('error');
}
});
}
</script>
PHP
// revoir les espaces dans les noms des tournois
/**
* @ParamConverter("tournament", options={"mapping": {"tournament_name": "name"}})
*/
public function showAction(Tournament $tournament) {
return $this->render('BasketTournamentBundle:Tournament:show.html.twig', array(
'tournament' => $tournament
));
}
public function updateAction($game_id, $team_id, $score) {
$em = $this->getDoctrine()->getManager('tournament');
$game = $em
->getRepository('BasketTournamentBundle:Tournament')
->findOneBy($game_id);
if ($team_id == $game->getTeam1()) {
$game->setScoreTeam1($score);
$em->persist($game);
$em->flush();
} else {
$game->setScoreTeam2($score);
$em->persist($game);
$em->flush();
}
return new Response();
}
<h3>Planning</h3>
{% for game in step.games %}
{% if (game.team1 in pool.teams) or (game.team2 in pool.teams) %}
<p>{{ game.dateGame|date('h:m:s') }} : {{ game.team1.name }} <input type="text" onblur="updateResult(this.value, {{ game.id }}, {{ game.team1.id }})"> - <input type="text" onblur="updateResult(this.value, {{ game.id }}, {{ game.team2.id }})"> {{ game.team2.name }}</p>
{% endif %}
{% endfor %}
我在警报中很好地获取了数据。
我无法更新我的实体..