2017-11-17 22 views
-2

私は投稿のコメントを書くことができるプロジェクトのようなタイムラインを作っています。しかし、私はデータを挿入することができません。コメントは期待通りに表示されています。フォームの値はsave_comments.phpページに渡されません。AJAXを使用してmysqlデータベースにデータを挿入できません

は、以下のコード

config.phpの

<?php 

    $con=new PDO("mysql:host=localhost;dbname=posts&comments","root",""); 
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

?> 

のindex.php

<?php 
    include 'config.php'; 
?> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 




<?php 


$sql=$con->prepare("SELECT * FROM posts"); 
$sql->execute(); 
$results=$sql->fetchAll(PDO::FETCH_ASSOC); 
echo "<hr>"; 
foreach($results as $post){ 
    echo "<h2>".$post['post_title']."</h2><br>"; 
    echo $post['post_content']."<br>"; 
    ?> 
<br><b>Comments</b> 
<form id=comment_form_<?php echo $post['post_id']; ?> action=""> 
    <input type=hidden name=pid value=<?php echo $post['post_id']; ?> > 
    <p><input type=text name=cbody id="comment_body_<?php echo $post['post_id']; ?>" placeholder="Write comment" ></p> 
    <p><input type=text name= cauth id="comment_author_<?php echo $post['post_id']; ?>" placeholder="Your Name" ></p> 
    <p><button class="submit_comment" post_id="<?php echo $post['post_id']; ?>" >Submit</button> </p> 

</form> 
<p><button class="show_comments" post_id="<?php echo $post['post_id']; ?>">Show comments</button></p> 
<div id="comments_<?php echo $post['post_id']; ?>"> 


</div> 
    <?php 
    echo "<hr>"; 
} 
?> 
<script> 
    $(document).ready(function() { 
     $(".show_comments").click(function() { 
      var post_id=$(this).attr('post_id'); 
      $("#comments_"+post_id).load("comments.php", { 
       post_id:post_id 
      }); 
    }); 
     $(".submit_comment").on("click",function() { 
      var post_id=$(this).attr('post_id'); 
      $.ajax({ 
       data:$('#comment_form_'+post_id).serialize(), 
       url:'save_comments.php', 
       async : true, 
       success: function(){ 
        $("#comments_"+post_id).load("comments.php", { 
         post_id:post_id 
        }); 
       } 
      }) 
     }); 
    }); 

</script> 

comments.php

012です?

<?php 

include 'config.php'; 

$comment_body=$_POST['cbody']; 
$comment_author=$_POST['cauth']; 
$post_id=$_POST['pid']; 
$sql2=$con->prepare("INSERT INTO comments Values(NULL,:pid,:cauth,:cbody)"); 
$sql2->bindParam(':pid',$post_id); 
$sql2->bindParam(':cauth',$comment_author); 
$sql2->bindParam(':cbody',$comment_body); 
$sql2->execute(); 
?> 

PS save_comments.php

>

:私はあなたがPOSTとしてAJAX要求を定義し、そのされていません

+2

[ブラウザの開発者ツールでAJAX要求/応答を見たことがありますか?プロジェクトにjQueryライブラリを含めましたか?エラーが報告されていますか?あなたはこれをウェブサーバー上で実行していますか?](http://jayblanchard.net/basics_of_jquery_ajax.html) –

+0

何のデバッグをしましたか? –

+0

'$ .ajax'はデフォルトで' GET'を使います。 –

答えて

0

AJAX & jqueryのではまだ初心者ですPOST変数は送信されません。

$.ajax({ 
    data:$('#comment_form_'+post_id).serialize(), 
    url:'save_comments.php', 
    async : true, 
    method: 'POST', <---- ADD THIS 
    success: function(){ 
     $("#comments_"+post_id).load("comments.php", { 
      post_id:post_id 
     }); 
    } 
})