Help me figure out how to write to the database and output to the page without reloading the page, my thought is this when going by url, in my case, the root folder of the site is used by the CommentsController controller, its actionComment method calls the static methods of the model Comments :: addComents to write to the database Comments :: getComments () on output from the database. without ajax everything works fine with ajax adds to the page but does not write to the database, as I understand it, I have a problem with the url to the controller (I tried different connection options) Routing `
class Router{ private $routes; public function __construct(){ $routersPath = ROOT.'/config/routers.php'; $this->routes = include($routersPath); } private function getURI(){ if (!empty($_SERVER['REQUEST_URI'])) { return trim($_SERVER['REQUEST_URI'], '/'); } } public function run(){ $uri = $this->getUri(); foreach ($this->routes as $uriPattern => $path){ if (preg_match("~$uriPattern~", $uri)){ $internalRoute = preg_replace("~$uriPattern~", $path, $uri); $segments = explode('/', $internalRoute); $controllerName = array_shift($segments).'Controller'; $controllerName = ucfirst($controllerName); $actionName = 'action'.ucfirst(array_shift($segments)); $parameters = $segments; $controllerFile = ROOT . '/controllers/' . $controllerName . '.php'; if (file_exists($controllerFile)){ include_once ($controllerFile); } $controllerObject = new $controllerName; $result = $controllerObject->$actionName($parameters); if ($result != null){ break; } } } } }` Routing
return array( '' => 'comments/comment'); Here is the controller code
class CommentsController { public function actionComment() { if (isset($_POST['submit'])){ $name = $_POST['name']; $email = $_POST['email']; $comments = $_POST['comments']; $result = Comments::addComents($name, $email, $comments); } $commentsList = Comments::getComments(); require_once(ROOT. '/views/comments/comments.php'); return true; } } MODEL
class Comments { public static function addComents($name, $email, $comments){ $db = Db::getConnection(); $sql = 'INSERT INTO user_comments (name, email, comments) ' .'VALUES (:name, :email, :comments)'; $result = $db->prepare($sql); $result->bindParam(':name', $name, PDO::PARAM_STR); $result->bindParam(':email', $email, PDO::PARAM_STR); $result->bindParam(':comments', $comments, PDO::PARAM_STR); $result->execute(); } public static function getComments(){ $db = Db::getConnection(); $commentsList = array(); $sql = 'SELECT id, name, email, comments ' . 'FROM user_comments ' . 'ORDER BY id DESC ' . 'LIMIT 9'; $result = $db->query($sql); $i = 0; while($row = $result->fetch()){ $commentsList[$i]['name'] = $row['name']; $commentsList[$i]['email'] = $row['email']; $commentsList[$i]['comments'] = $row['comments']; $i++; } return $commentsList; } } INDEX
$router = new Router(); $router->run(); The form
<form action="" method="POST" id="form-message"> <div class="form-group-wrapper"> <div class="form-group"> <label for="name">Имя</label> <input type="text" class="form-control" id="name" name="name"> <label for="email">E-Mail</label> <input type="email" class="form-control" id="email" name="email"> </div> </div> <div class="form-group form-group-flex"> <label for="comments">Комментарий</label> <textarea class="form-control" id="comments" rows="" name="comments"></textarea> </div> <div class="col-xs-12 text-right reset-padding"> <button type="submit" name="submit" class="btn btn-danger" id="btn-submit">Записать</button> </div> </form> Form validation and Ajax request
$(document).ready(function(){ $('#form-message').on('submit', function(e){ e.preventDefault(); console.log('asfd') var form = $(this); var name = $('#name'); var email = $('#email'); var comments = $('#comments'); if(name.val() == ''){ name.css("border", "1px solid red"); return; } else { name.css("border", "1px solid transparent"); } if(email.val() == ''){ email.css("border", "1px solid red"); } else { var r = /^([a-z0-9_\.-])+@[a-z0-9-]+\.([az]{2,4}\.)?[az]{2,4}$/i; if (!r.test(email.val())) { email.css("border", "1px solid red"); return; } else { email.css("border", "1px solid transparent"); } } if(comments.val() == ''){ comments.css("border", "1px solid red"); return; } else { comments.css("border", "1px solid transparent"); } $.ajax({ type: "POST", async: false, url: "../../controllers/Comments", data: 'email='+email.val()+'&name='+name.val()+'&comments='+comments.val(), dataType: "html", success: function(data) { console.log('success') $('.message-container').append( '<div class="col-md-4 col-sm-6 col-xs-12 comment-item">'+ '<div class="item text-center">'+ '<div class="item-header">' + name.val() + '</div>' + '<div class="item-body">' + '<div class="item-body-email">'+ email.val() +'</div>' + '<div class="item-body-message">' + comments.val() + '</div>'+ '</div>'+ '</div>' + '</div>' ); //$form.trigger( 'reset'); } }); }) });