0

I ask you because I am stuck on a problem (I have previously searched).

I would like, from an input field, update the database without the user noticing.

My code :

<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 %}

I get my data well in the alert.

I can not update my entity..

  • Which input field? Are you wanting us to guess what you're trying to do? – Jay Blanchard May 20 '16 at 12:21
  • This input in my html file : `` What I try to do : update the game table in the database with the score without the user having to click somewhere – Alex Bortoluzzi May 20 '16 at 12:23
  • What does not work ? – Sofiene Djebali May 20 '16 at 12:28
  • as commented previously it is a bit unclear what part exactly is the thing that is not working for you. But couple of questions and pointers for you to check: do you have the route defined correctly? is your controller action missing the 'Action' keyword intentionally? Shouldn't you be returning JSONResponse if you are calling the action via ajax? Should you be using get parameters on the route if you want to use post option (or other way around)? You could also directly check and copy logic from http://stackoverflow.com/questions/13584591/how-to-integrate-ajax-with-symfony2/13585748#13585748 – ejuhjav May 20 '16 at 12:51
  • Yes sorry. I have missing 'Action' keyword. In my code , the updated database does is simply not.. – Alex Bortoluzzi May 20 '16 at 13:20

0 Answers0