Project: link
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 poll questions I have stored in the database and an array containing the responses that the user selects. At each click on a star it makes the request by sending it to ..app / Controllers / Poll.php
Once there, I need to do an insert or update, but ... ... the problem is that it does not recognize my Controller class or 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";
}
}
}
?>