The jquery query looks like this.


<script> function post(){ $.post("pages/comment.php?id=<?echo$row['id'];?>", { text<?echo$row['id'];?>: $("#text<?echo$row['id'];?>").val() }, function(data) { if(data != "ok"){ $("#error_error").html("<div class=\"alert alert-error\"><a class=\"close\" data-dismiss=\"alert\" href=\"#\">Γ—</a>"+data+"</div>"); }else{ location.replace("/articles.php?id=<?echo$row['id'];?>"); } }); } </script> 

File handler


 require_once '../system/core.php'; $row = mysql_fetch_assoc(mysql_query("SELECT * FROM `articles` WHERE `id`='".$id."' AND `moderation`='1'")); $text=htmlspecialchars(trim($_POST['text'])); if (empty($text)) { echo 'НС Π²Π²Π΅Π΄Ρ‘Π½ тСкст коммСнтария!'; exit; } if (mb_strlen($text) < 2 || mb_strlen($text) > 1000) { echo 'НСдопустимая Π΄Π»ΠΈΠ½Π° тСкста коммСнтария'; exit; } //Ссли Π½Π΅Ρ‚ ошибок - пишСм Π² Π±Π°Π·Ρƒ mysql_query("UPDATE `articles` SET `comments` = '".intval($row['comments']+1)."' WHERE `id` = '".$row['id']."'"); mysql_query("INSERT INTO `articles_comments` SET `id_user` = '".$user['id']."', `id_articles` = '".$row['id']."', `text` = '".mysql_real_escape_string($text)."', `time` = '".$realtime."'"); $rating_user = $user['rating']+0.05; if($user['rating']<$setup['rating'])mysql_query("UPDATE `users` SET `rating`='".$rating_user."' WHERE `id`='".$user['id']."'"); echo "ok"; 

The form


 echo '<div id="error_error"></div>'; echo '<textarea id="text'.$row['id'].'" rows="5" placeholder="Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ваш ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ"></textarea> <input type="button" class="blue" value="Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ" onclick="post()"><br/>'; 

So, you need to somehow add the article ID to the post-request. On one page I have a certain number of articles. Each article has comments.

    2 answers 2

     function post(){ //ΠΏΡ€ΠΈΠΌΠ΅Ρ€ для послСдних вСрсий jquery $.ajax({ type: "POST", url: "pages/comment.php", data: { id: <?php echo $row['id'] ?> } }).done(function( data ) { //html ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€ Π½Π° страницС, ΠΊΡƒΠ΄Π° Ρ‚Ρ‹ Ρ…ΠΎΡ‡Π΅ΡˆΡŒ ΠΏΠΎΠΌΠ΅ΡΡ‚ΠΈΡ‚ΡŒ ΠΎΡ‚Π²Π΅Ρ‚ $('#comments').html(data); }); } 

    In total, the Ajax function sends data to the script and expects a response from it, which places it in the container with id = "comments", respectively, the php processing script should be a separate file that should output the answer in html format.

    By and large, the entire server response would be correctly translated into json and used on the client side to process error messages, etc.

    • What exactly should be added to the handler? Added $ ("# comments"). Html (data) .show (). Delay (1500) .fadeOut (800) to that function; so that the error message disappears. And the inscription is always shown that the comment text is not entered. - vitagame
    • I did this: <script> function post () {// Get the parameters var text = $ ('# text'). Val (); // display information about sending data $ ("# load"). fadeIn ('slow'); // Send the parameters $ .ajax ({type: "POST", url: "pages / comment.php? Id = <? Echo $ row ['id'];?>", Data: "text =" + text, // Display what PHP returns. Success: function (html) {$ ("# result"). Append (html); $ ("# result"). SlideDown ('slow'); $ ("# result"). text (html) .show (). delay (1500) .fadeOut (800); // Remove information about sending data $ ("# load"). fadeOut ('slow');}}); } </ script> But until I update the page the comment will not appear. - vitagame
    • The function called in case of a successful server response is a callback that should work automatically, if this does not happen, it means that something is wrong in the code, try debugging. The structure is as follows - the script displays the list of old comments and the form for adding new ones. The second script processes requests for adding comments and sends a positive or negative response. On the page with a list of comments, javascript makes requests to the second script and processes its answers, in our case it adds the contents of the answer to the necessary container on the page. - zippp
    • ie the user's comment was "Cool", javascript pulled the user id, passed it with the text of the comment in php to the second script, which writes it to the database and returns an {errors: false type response; comment: 'Cool'; author: 'Vasya', time: '02 .11.12 '}, javascript gets this answer (in my example in the done () method) and reads the resulting json, checks for errors (data.errors). If there is an error, the error body is written to the error container $ ('# errors'). Text (data.errors). If there are no errors, a block of comments is formed and added to the general list of comments. - zippp
     $.ajax({ type: 'POST', url: url, data: data, success: success, dataType: dataType }); 
    • And how to apply it? - vitagame
    • And in my case, comments are added with the page refresh. How to avoid it? - vitagame