2016-04-10 9 views
0

Ich habe ein Kommentar-System auf meinem Blog hinzugefügt, das Problem ist, dass ich keinen Kommentar in die Datenbank einfügen kann, denke ich, weil es nicht die Post-ID von erhalten kann der aktuelle Blog Aus comments.php werden die Daten an post_comments.php übergeben. Wie kann ich die Post-ID auf dem Ajax-Teil (unter dem Skript-Tag) übergeben? Das ist das erste Mal, dass ich Ajax benutze, also bin ich nicht wirklich gut damit. Jede Hilfe wird sehr geschätztWie bekomme ich die aktuelle Seite ID in einem Kommentar-System mit AJAX

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; 
    } 
?> 

Antwort

1

Sie müssen post_id passieren .So Sie post_id zu einem versteckten speichern müssen field.And überprüfen Sie es in Ajax Call-Code.

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

Sorry, ich vergaß fälschlicherweise name = "post_id" in verstecktes Feld zu setzen –

+0

danke:) ist dies perfekt – grasig

+0

Sie sind herzlich willkommen. –

Verwandte Themen