View Single Post
  #1  
Old 16th April 2020, 19:10
geeb21's Avatar
geeb21 geeb21 is offline
Member
 
Join Date: Jan 2012
P2P
Posts: 2
Default News comment ajax script.
Hy please help.



Mysql table:


CREATE TABLE `news` (
`id` int(10) UNSIGNED NOT NULL,
`datetime` date DEFAULT NULL,
`title` varchar(120) COLLATE latin2_hungarian_ci DEFAULT NULL,
`content` text COLLATE latin2_hungarian_ci,
`showmode` enum('block','none') COLLATE latin2_hungarian_ci NOT NULL DEFAULT 'block'
) ENGINE=MyISAM DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci PACK_KEYS=0;

CREATE TABLE `news_comment` (
`comment_id` int(11) NOT NULL,
`parent_comment_id` int(11) NOT NULL,
`nid` int(11) NOT NULL,
`comment` text CHARACTER SET utf8,
`comment_sender_name` varchar(40) CHARACTER SET utf8 NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci PACK_KEYS=




add_comment:


PHP Code:
<?php//add_comment.php$connect = new PDO('mysql:host=localhost;dbname='', '', '');$error = '';$comment_name = '$nev';$comment_content = '$comment_content';if(empty($_POST["comment_name"])){ $error .= '<p class="text-danger">Név hibádzik..</p>';}else{ $comment_name = $_POST["comment_name"];}if(empty($_POST["nid"])){ $error .= '<p class="text-danger">Hír kapcsolás</p>';}else{ $nid = $_POST["nid"];}if(empty($_POST["comment_content"])){ $error .= '<p class="text-danger">Komment hibádzik</p>';}else{ $comment_content = $_POST["comment_content"];}if($error == ''){ $query = " INSERT INTO news_comment  (parent_comment_id, ,nid, comment, comment_sender_name)  VALUES (:parent_comment_id, :nid, :comment, :comment_sender_name) "; $statement = $connect->prepare($query); $statement->execute(  array(   ':parent_comment_id' => $_POST["comment_id"],   ':nid' => $_POST["$news.id"],   ':comment'    => $comment_content,   ':comment_sender_name' => $comment_name  ) ); $error = '<label class="text-success"><font color="yellowgreen">Hozzászóltál..</font></label>';}$data = array( 'error'  => $error);echo json_encode($data);?>

fetch_commet.php


PHP Code:
<head>    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">  </head><?php$connect = new PDO('mysql:host=localhost;dbname='', '', '');$connect->exec("set names utf8");$query = mysql_query("SET NAMES latin2");$query = "SELECT * FROM news_comment WHERE parent_comment_id = '0' ORDER BY comment_id ";$statement = $connect->prepare($query);$statement->execute();$result = $statement->fetchAll();$output = '';foreach($result as $row){ $output .= '<div style=" border-collapse: collapse; border: none;  width:482px; padding:4px 0px 0px 0px;  text-align: left; float:left;">    <div style="padding: 2px" ;=""></div>    <div id="k_hsz_6045">         <div style="display:block; width:552px; text-align: center; font-weight:bold;   margin: 0 auto 0 auto;">          <div style="padding:0px 0px 0px 4px;">                      <div style="display:block; height:70px; float:left; width:58px;">                <div style=" padding:4px 0px 0px 0px;">                                       <img src="themes/default/avatar.jpg" width="50" height="50">                              </div>          </div>                           <div id="k_hsz2_6045">    <div style=" border-collapse: collapse; border: none;  width:482px; padding:4px 0px 0px 0px;  text-align: left; float:left;">            <div id="hsz_buberek_eleje_sotet">        <div style="font-size: 7pt; padding:7px 0px 0px 25px;">                    <div style="display:block;  float:left;">             <b><span style="color:#74FF04">Írta: </span> <span style="color:#00FF6C">'.$row["comment_sender_name"].'</span><font color="yellow">&nbsp;&nbsp;&nbsp;&nbsp;Mikor:  '.$row["date"].'</b></font>             </div>                </div>     </div>          <div id="hsz_buborek_kozepe_sotet">                  <div style="font-size: 10pt; padding:0px 7px 0px 20px;"><font color="white">'.$row["comment"].'  </div>          </div>          <div id="hsz_buborek_vege_sotet">            </div>                   <div style="padding: 2px" ;=""></div>          </div>              <div id="k_hsz_6048">         '; $output .= get_reply_comment($connect, $row["comment_id"]);}echo $output;function get_reply_comment($connect, $parent_id = 0, $marginleft = 0){ $query = " SELECT * FROM news_comment WHERE parent_comment_id = '".$parent_id."' "; $output = ''; $statement = $connect->prepare($query); $statement->execute(); $result = $statement->fetchAll(); $count = $statement->rowCount(); if($parent_id == 0) {  $marginleft = 0; } else {  $marginleft = $marginleft + 48; } if($count > 0) {  foreach($result as $row)  {   $output .= '   <div class="panel panel-default" style="margin-left:'.$marginleft.'px">    <div class="panel-heading">Írta <b>'.$row["comment_sender_name"].'</b> on <i>'.$row["date"].'</i></div>    <div class="panel-body">'.$row["comment"].'</div>    <div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Válasz</button></div>   </div>   ';   $output .= get_reply_comment($connect, $row["comment_id"], $marginleft);  } } return $output;}?>

Index.php:


PHP Code:
<?php        $newsdata mysql_query("SELECT * FROM `news` ORDER BY `datetime` DESC;");        if(mysql_num_rows($newsdata) == 0) {            section_begin("<img src='HU.gif' height='16' width='30' />&nbsp;&nbsp;Hírek"1);            echo("<center> <img src='hirek.png' alt='' > </center>");            echo "<p>Semmi újdonság...</p>";            section_end();        } else {            section_begin("<img src='HU.gif' height='16' width='30' />&nbsp;&nbsp;Hírek"1);            echo "<table width="100%">".$linebreak;            if($szint >= 8) {?><center> <img src="crach.gif" height="90" width="90"><img src="hirek.png" alt="" ><img src="giphy.gif" height="90"></center>    <tr>        <td><a href="news_add.php" title="Új hír"><img border="0" src="themes/<?=$theme?>/news_add.gif" alt=""/>&nbsp;<i>Új hír hozzáadása</i></a><br/></td>    </tr>    <tr>        <td height="5"></td>    </tr><?php            }            while($news mysql_fetch_array($newsdata)) {                if($szint >= 8$editlink " | <a href="news_edit.php?article={$news['id']}" title="Szerkesztés">Szerkesztés</a> | <a href="delete.php?article={$news['id']}" title="Törlés">Törlés</a>";                if($news['showmode'] == 'block')                    $buttonpic 'collapse';                else                    $buttonpic 'expand';?>    <tr>        <td height="10"></td>    </tr>    <tr>        <td>            <p class="left"><a href="javascript:collapse('<?=$news['id']?>')"><img border="0" src="themes/<?=$theme?>/news_<?=$buttonpic?>.gif" id="button<?=$news['id']?>" alt=""/>&nbsp;<i><?=$news['datetime']?></i>&nbsp;-&nbsp;<b><? echo bb2html($news['title'], 0); ?></b></a><?=$editlink?></p>            <div class="news" id="article<?=$news['id']?>" style="display: <?=$news['showmode']?>;">            <? echo bb2html($news['content'], 0); ?>            </div><center>    <?php?><div id="hozzaszolas_tabla"> <head>  <script src="bootstrap/3.3.6/js/jquery.min.js"></script>  <script src="bootstrap/3.3.6/js/bootstrap.min.js"></script>   </head> <script>$(document).ready(function(){  $('#comment_form').on('submit', function(event){  event.preventDefault();  var form_data = $(this).serialize();  $.ajax({   url:"add_comment.php",   method:"POST",   data:form_data,   dataType:"JSON",   success:function(data)   {    if(data.error != '')    {     $('#comment_form')[0].reset();     $('#comment_message').html(data.error);     $('#comment_id').val('0');     load_comment();    }   }  }) }); load_comment(); function load_comment() {  $.ajax({   url:"fetch_comment.php",   method:"POST",   success:function(data)   {    $('#display_comment').html(data);   }  }) } });</script>    <?php$nid = $news['id'];?><div style="margin-left:10px; margin-top:4px;">     <span id="comment_message"></span>   <br />   <div id="display_comment"></div><div style="padding: 1px" ;=""></div>   <div style=" border-collapse: collapse;  border: none;  width:545px; text-align: center;  margin: 0 auto 0 auto;  ">         <div id="elvalaszto_csik_1"></div>     <div id="elvalaszto_csik_2"></div>         </div><br><form method="POST" id="comment_form" action="add_comment.php">    <div class="form-group">     <input type="hidden" name="comment_name" id="comment_name"  value="<?php echo $nev ?>" />    <input type="hidden" name="nid" id="nid"  value="<?php echo $nid ?>" />    </div>    <div class="form-group">       <textarea style="height: 70px;width:541px; border-radius:6px;  resize: vertical; font-weight: bold; font-family: verdana,tahoma; font-size: 8pt; background-color:#D3D7DC;" name="comment_content" id="comment_content"  placeholder="Hozzászólok ehhez a hírhez..." rows="5" maxlength="500" onkeyup="countChar(this)"></textarea>        <div style="margin-left:10px; margin-top:4px;">     <input type="hidden" name="comment_id" id="comment_id" value="0" />     <input type="image" name="submit" id="submit"  value="Submit" src="/gombok/mehot.png" style="height:20px; width=40px" />    </div>   </form>    <font color="orange" size="2"><i>(Max 500 karakter)</i></font>     </div><br><?php                }            echo "</table>".$linebreak;                section_end();        }        echo "<br>";echo "</div>";    page_end();    }}

Not listen very news_id and not send comment database. Please help. THx
Reply With Quote