ajaxにおける相対パスのが繋がらず非同期処理が出来ない状況です。
下記画像の❤️のいいねボタンを設置しようとしているのですが、
require_once(/Applications/MAMP/Views/Users/Good.php): failed to open stream: No such file or directory in /Applications/MAMP/public/index.php on line 8
とのエラーが出てしまいます。
good.jsのajax相対パスのurlをGood.php'→../Good.php'に変更して試しましたが、
require_once(/Applications/MAMP/Views/Good.php): failed to open stream: No such file or directory in /Applications/MAMP/public/index.php on line 8とエラーがでしまい../../Good.phpに変更してエラー内容は変わらないです。このエラーの解決方法をご教示頂きたいです。
よろしくお願い致します。
index.php
<?php define('ROOT_PATH', str_replace('public', '', $_SERVER["DOCUMENT_ROOT"])); $parse = parse_url($_SERVER["REQUEST_URI"]); //ファイル名が省略されたいた場合、index.phpを補填する if (mb_substr($parse['path'], -1) === '/') { $parse['path'] .= $_SERVER["SCRIPT_NAME"]; } require_once(ROOT_PATH.'Views'.$parse['path']);
good.js
$(function() { //いいね機能 var $good = $('.btn-good'),//いいねボタンセレクタ goodPostId;//レシピID //カスタム属性(postid)に格納された投稿ID取得 $good.on('click',function(e) { e.stopPropagation(); var $this = $(this); //カスタム属性(postid)に格納された投稿ID取得 goodPostId = $this.parents('.post').data('postid'); //レシピID取得 $.ajax({ url:'../Good.php',//post送信を受けとるphpファイル type:'POST', data:{post_id: goodPostId}//{キー:レシピID} }).done(function(data){ // いいねの総数を表示 $this.children('span').html(data); // いいね取り消しのスタイル $this.children('i').toggleClass('far'); //空洞ハート // いいね押した時のスタイル $this.children('i').toggleClass('fas'); //塗りつぶしハート $this.children('i').toggleClass('active'); $this.toggleClass('active'); }).fail(function(msg) { //console.log('Ajax Error'); }); }); });
good.php
<?php require_once(ROOT_PATH .'/Models/Db.php'); class Good extends Db { public function __construct($dbh = null) { parent::__construct($dbh); } public function good($user_id, $recipe_id) { try { $sql = 'SELECT *FROM goodsWHERE recipe_id = :recipe_id AND user_id = :user_id'; $stmt = $this->dbh->prepare($sql); $params = array( ':user_id' => $user_id, ':recipe_id' => $recipe_id ); $stmt -> execute($params); $resultCount = $stmt -> rowCount(); // レコードが1件でもある場合 if (!empty($resultCount)) { // レコードを削除する $sql = 'DELETE FROM goods WHERE recipe_id = :recipe_id AND user_id = :user_id'; $stmt = $this -> dbh -> prepare($sql); $params = array( ':user_id' => $user_id, ':recipe_id' => $recipe_id ); $stmt -> execute($params); echo count($this -> getGood($recipe_id)); } else { // 1件もなかったらレコードを挿入する $sql = 'INSERT INTO goods (recipe_id, user_id)VALUES (:recipe_id, :user_id)'; $stmt = $this -> dbh -> prepare($sql); $params = array( ':recipe_id' => $recipe_id, ':user_id' => $user_id ); $stmt -> execute($params); echo count($this -> getGood($recipe_id)); } } catch (PDOException $e) { exit($e); } } //いいねした情報があるか確認 public function isGood($user_id, $recipe_id) { try { $sql = 'SELECT * FROM goods WHERE recipe_id = :recipe_id AND user_id = :user_id'; $sth = $this -> dbh -> prepare($sql); $sth -> bindParam(':user_id', $user_id, \PDO::PARAM_INT); $sth -> bindParam(':recipe_id', $recipe_id, \PDO::PARAM_INT); $sth -> execute(); return $sth; } catch (Exception $e) { error_log('エラー発生:' . $e->getMessage()); } } //goodsテーブルからレシピID一致したいいね取得をするSQL文 public function getGood($recipe_id) { $sql = 'SELECT * FROM goods WHERE recipe_id = :recipe_id'; $sth = $this -> dbh -> prepare($sql); $sth -> bindParam(':recipe_id', $recipe_id, \PDO::PARAM_INT); $sth -> execute(); $result = $sth -> fetch(PDO::FETCH_ASSOC); return $result; } }
myrecipe.php
<?php session_start(); require_once(ROOT_PATH .'/Controllers/UserController.php'); if (empty($_SESSION["login_user"])) { header('Location: login_form.php'); } $recipe = new UserController(); $view = $recipe -> view(); $view_process = $recipe -> method(); $count_good = $recipe -> getGood($_GET['id']); $recipe_id = $view["recipe"]["id"]; //投稿ID $dbPostGoodNum = ''; //いいねの数 if (!empty($_GET['id'])) { // 投稿IDのGETパラメータを取得 $recipe_id = $_GET['id']; // DBからいいねの数を取得 $dbPostGoodNum = $count_good; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/css/bootstrap.css" type="text/css"/> <link rel="stylesheet" href="/css/form.css" type="text/css"/> <href="https://use.fontawesome.com/releases/v5.15.4/css/all.css"> <script src="../js/jquery-3.6.0.min.js" type="text/javascript"></script> <script src="../js/good.js" type="text/javascript"></script> <script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js"></script> <title>レシピ画面詳細</title> </head> <body style = "background-color :#FAEBD7;"> <?php include("header.php")?> <div class = "box"> <div id = "recipe_box" class = "my-5 d-flex align-items-center justify-content-center" > <div id = "recipe_left" style = "width:35%; height:250px;"> <div id = "image_form"> <img src = "<?php echo "/". $view["recipe"]["file_path"]?>" style = "width:80%;" class = "mx-4"> </div> <section style = "width:70%; height:20%;" class = "post mx-5" data-postid = <?php echo $recipe_id ?>> <div style = "width:100%; height:70%;" class="btn-good <?php if ($recipe-> isGood($view["recipe"]["user_id"], $_GET['id'])) { echo "active"; }?>"> <!-- 自分がいいねした投稿にはハートのスタイルを常に保持する --> <i style = "width:100%; height:100%;" class="fa-heart fa-lg px-16 <?php if ($recipe -> isGood($view["recipe"]["user_id"], $view["recipe"]["id"])) {//いいね押したらハートが塗りつぶされる echo 'active fas'; } else { //いいねを取り消したらハートのスタイルが取り消される echo 'far'; }; ?>"></i><span><?php echo $dbPostGoodNum; ?></span> </div> </section> </div> <div id = "recipe_right" style = "width:50%;" class = "mx-2"> <div id = "recipe_form"> <label for="name">タイトル</label><br> <p><?php echo $view["recipe"]["name"];?></p> </div> <div id = "recipe_form"> <label for="intro">ひとこと紹介</label><br> <p><?php echo $view["recipe"]["introduce"];?></p> </div> <div class = "d-flex" style = "width:100%;"> <div id = "recipe_form_figure"> <label for="time">調理時間</label><br> <p><?php echo $view["recipe"]["time"];?>分</p> </div> <div id = "recipe_form_figure" class = "mx-5"> <label for="cost">費用</label><br> <p><?php echo $view["recipe"]["cost"];?>円</p> </div> <div id = "recipe_form_figure"> <label for="cost">目安</label><br> <p><?php echo $view["recipe"]["serving"];?>人前</p> </div> </div> </div> </div> </div> <?php include("footer.html")?> </body> </html>
UserController.php
<?php require_once(ROOT_PATH .'/Models/Good.php'); class UserController { private $request; //リクエストパラメータ(GET,POST) private $User; //Userモデル private $Recipe; //Recipeモデル private $Process; //Processモデル private $Image; //Imagesモデル private $Good; //Goodモデル public function __construct() { //インスタンス化で最初に走る処理 //get['id']とpostはrequestに入る $this -> request['get'] = $_GET; $this -> request['post'] = $_POST; //モデルオブジェクトの生成 $this -> User = new User(); $dbh = $this -> User -> getDbHandler(); $this -> Recipe = new Recipe($dbh); $this -> Process = new Process($dbh); $this -> Image = new Image($dbh); $this -> Good = new Good($dbh); } //Goodsテーブル public function good($user_id, $recipe_id) { $good = $this -> Good -> good($user_id, $recipe_id); } public function getGood($recipe_id) { $get_good = $this -> Good -> getGood($recipe_id); $params = $get_good; return $params; } public function isGood($user_id, $recipe_id) { $is_good = $this -> Good -> isGood($user_id, $recipe_id); $params = $is_good; return $params; }
まだ回答がついていません
会員登録して回答してみよう