AJAX with Slim Frame

Asked

Viewed 85 times

0

Project : https://github.com/noebezerra/survey-slim

I have a problem with the AJAX request ( Resources/views/templates/app.Twig )

ajax - app.Twig

$('.stars-default').click(function(e) {
    e.preventDefault();
    $.ajax({
        url: '../app/Controllers/Poll.php',
        type: 'GET',
        dataType: 'html',
        data: {valor: pointStar, qtdperguntas: qtdperguntas},
    })
    .done(function(data) {
        console.log(data);
    })
    .fail(function() {
        console.log("error");
    })
});

It sends the number of questions of the poll I have stored in the database and an array containing the answers that the user selects. With each click on a star he makes the request by sending it to .. app/Controllers/Poll.php

When I get there I need to do an update or update, but... ...the problem is that it does not recognize my Controller class and neither does Pollanswers.

Poll.php

<?php 
    namespace App\Controllers;
     use App\Models\PollAnswers;
     session_start();
     /**
     *
     */
     class Poll extends Controller {
         public function poll() {
            $qtdperguntas = $_GET['qtdperguntas'];
            $result = '[';
            for ($i=0; $i < $qtdperguntas; $i++) { 
                if ($i < $qtdperguntas - 1) {
                    $result .= '"'.$_GET['valor'][$i].'",';
                } else {
                    $result .= '"'.$_GET['valor'][$i].'"';
                }
            }
            $result .= ']';
            $userpoll = PollAnswers::where('id_user', '=', $_SESSION['user']);
            if (!$userpoll) {
                PollAnswers::create([
                    'id_user' => $_SESSION['user'],
                    'answers' => $result
                ]);
                echo "insert";
            } else {
                $userpoll->answers = $result;
                echo "update";
            }
        }
     }
?>
  • Someone is there? Help me

  • Got it. The solution is that you need to create a route and have ajax access this route.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.