2016-04-10 23 views
0

私のブログにコメントシステムを追加しました。問題は、データベースにコメントを挿入できないことです。現在のブログcomments.phpから、データはpost_comments.phpに渡されます。どうすればajaxの部分(scriptタグの下)にpost_idを渡すことができますか?これは初めてのAJAXを使用しているので、本当にうまくいきません。すべてのヘルプははるかに高く評価されますAJAXを使用してコメントシステムで現在のページIDを取得する方法

comments.php:

<form method='post' action="" onsubmit="return post();"> 
<textarea id="comment" placeholder="Write Your Comment Here....."></textarea> 
<br> 
<input type="text" id="username" placeholder="Your Name"> 
<br> 
<input type="submit" value="Post Comment"> 
</form> 

<div id="all_comments"> 
<?php 
    $host="localhost"; 
    $username="root"; 
    $password=""; 
    $databasename="comments"; 

    $connect=mysql_connect($host,$username,$password); 
    $db=mysql_select_db($databasename); 

    $comm = mysql_query("select name,comment,post_time from comments order by post_time desc"); 
    while($row=mysql_fetch_array($comm)) 
    { 
     $name=$row['name']; 
     $comment=$row['comment']; 
     $time=$row['post_time']; 
     ?> 
     <div class="comment_div"> 
     <p class="name">Posted By:<?php echo $name;?></p> 
     <p class="comment"><?php echo $comment;?></p> 
     <p class="time"><?php echo $time;?></p> 
     </div> 
     <?php 
    } 
    ?> 
</div> 

post_comments.php

<?php 
    $host="localhost"; 
    $username="root"; 
    $password=""; 
    $databasename="comments"; 

    $connect=mysql_connect($host,$username,$password); 
    $db=mysql_select_db($databasename); 

    if(isset($_POST['user_comm']) && isset($_POST['user_name'])) 
    { 
     $comment=$_POST['user_comm']; 
     $name=$_POST['user_name']; 
     $insert=mysql_query("insert into comments values('','$name','$comment',CURRENT_TIMESTAMP)"); 
     $select=mysql_query("select name,comment,post_time from comments where name='$name' and comment='$comment' "); 

    if($row=mysql_fetch_array($select)) 
    { 
     $name=$row['name']; 
     $comment=$row['comment']; 
     $time=$row['post_time']; 
     ?> 

    <div class="comment_div"> 
     <p class="name">Posted By:<?php echo $name;?></p> 
     <p class="comment"><?php echo $comment;?></p> 
     <p class="time"><?php echo $time;?></p> 
    </div> 
<?php 
    } 
    exit; 
    } 
?> 
あなたが隠しに post_idを格納する必要があります。だからあなたは post_idを渡す必要が

答えて

1

電話番号Ajaxで確認してください。

comments.php

...... 
<input type="hidden" id="post_id" name="post_id" value="<?php echo $post_id?>"> 
<input type="submit" value="Post Comment"> 
...... 

post_comments.php

...... 
if(isset($_POST['user_comm']) && isset($_POST['user_name']) && isset($_POST['post_id'])) 
    { 
     $post_id=$_POST['post_id']; 
     // do what ever you wanted to do with post_id 
     $comment=$_POST['user_comm']; 

...... 
+0

私は誤って –

+0

ありがとう隠しフィールドに名前= "post_idのを" 入れるのを忘れて申し訳ありません:これは完璧です) – grasig

+0

あなたは大歓迎です。 –

関連する問題