0

Hello pls i am making a comment feature for my log.
I want the comments to be submited from a form in the "READ_MORE.PHP" , and i want the form to be processed in a "COMMENT.PHP" page were the form values are insertrd in the Comment table... (which has 5 fields - id,name,comment,post_id,date)
I already made a code for that. its working but the onlyy problem is that the post_id is not having any value its always 0. So i think it might be that i didn't get the post_id from the "READ_MORE.PHP" page...
So please someone pls help me... Below are my codes:

I have attatched the READ_MORE and COMMENT pages to this post

Thanks!!!

Attachments
<?php
//include db connection file
require_once 'admin/dbconnect.php';

?>



<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        
        <link rel="stylesheet" href="css/font-awesome.min.css">
        
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <link  rel="stylesheet" href="css/greencss.css">
        <link rel="stylesheet" href="css/comment.css"/>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="jquery.min.js"></script>
        
        

        <title>Green Blog | <?php echo $ViewTitle; ?></title>
    </head>
    <body class="news">

        <?php include 'includes/header.php'; ?>

        <!-- submit news modal -->
        <div class="container">
            <h2></h2>

        </div>
        
        <?php
        $sel = "SELECT * FROM blogposts WHERE post_id=".$_GET['post_id'];
        $resView = mysql_query($sel);
        if(mysql_num_rows($resView) > 0){
            
        
        while ($row1 = mysql_fetch_array($resView)) {
            $ViewId = $row1['post_id'];
            $ViewTitle = $row1['post_title'];
            $ViewCont = $row1['post_content'];
            $ViewAuthor = $row1['post_author'];
            $ViewDate = $row1['post_date'];

        ?>
        <div class="container-fluid" style="margin-top: 50px;">
            
            <div class="row" style="margin-top: 100px;">
                
                <div class="col-md-8"><!-- Start of One full post col -->
                    <div class="FullPostDiv">
                        
                        
                        <h2 class="text-success text-uppercase"><?php echo $ViewTitle; ?></h2>
                        <br/>
                        
                        <p class="pFullPost">
                        
                            <?php echo $ViewCont; ?>
                        
                        </p>
                        
                        <br/>
                        
                        <div>
                            
                            <!-- Begin Comments JavaScript Code --><script type="text/javascript" async>function ajaxpath_5951a68111fad(url){return window.location.href == '' ? url : url.replace('&s=','&s=' + escape(window.location.href));}(function(){document.write('<div id="fcs_div_5951a68111fad"><a title="free comment script" href="http://www.freecommentscript.com">&nbsp;&nbsp;<b>Free HTML User Comments</b>...</a></div>');fcs_5951a68111fad=document.createElement('script');fcs_5951a68111fad.type="text/javascript";fcs_5951a68111fad.src=ajaxpath_5951a68111fad((document.location.protocol=="https:"?"https:":"http:")+"//www.freecommentscript.com/GetComments2.php?p=5951a68111fad&s=#!5951a68111fad");setTimeout("document.getElementById('fcs_div_5951a68111fad').appendChild(fcs_5951a68111fad)",1);})();</script><noscript><div><a href="http://www.freecommentscript.com" title="free html user comment box">Free Comment Script</a></div></noscript><!-- End Comments JavaScript Code -->
                            
                        </div>
                        
                    </div>
                    
                            <?php
                            if('POST' == $_SERVER['REQUEST_METHOD']) {
                                
                            $sql = "UPDATE gr_likes set `likes` = `likes`+1 where `post_id` =".$_GET['post_id'];
                            $result=mysql_query($sql);
                            
                            if($sql){//if 
                                $sql1 = "SELECT * FROM gr_likes WHERE post_id".$_GET['post_id'];
                                $result1 = mysql_query($sql1);
                                if(mysql_num_rows($result1) > 0){
                                    
                                    while ($row2 = mysql_fetch_row($result1)) {
                                        $likeNo = $row2['likes'];
                                    }
                                }
                            }
                            }
                                ?>
                    
                    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" style="margin-left: 30px;">
                        <button type = "submit" class="btn btn-lg btn-success" name='like'><i class="fa fa-thumbs-o-up"></i>Like</button>
                        <span class=""><?php echo $likeNo; ?></span>
                    </form>
                    
                    <br/>
                    <div style="margin-left: 30px;">
                        <h3 class="text-success text-uppercase">Drop Your Comment</h3>
                        
                        
                        <form method="POST" name="form" action="comment.php">
                            
                            <div class="form-group">
                                <input class="form-control" type="text" id="name" name="name" placeholder="Your name..." />
                            </div>
                            <div class="form-group">
                                <textarea name="comment" id="message" class="form-control" placeholder="Your comment..."></textarea>
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-success" name="submit">Comment</button>
                            </div>
                            
                        </form>
                        
                        <?php
                        
                        ?>
                        
                        <div class="space">
                            
                            
                            
                        </div>
                        
                    
                    </div>
                </div><!-- End of Full post col -->
                
                <div class="col-md-4"><!--Start of Sidebar col-->
                    
                    <?php
                    
                    include 'sidebar.php';
                    ?>
                    
                </div><!-- End of Sidebar COL-->
                
            </div>
            
        </div>
        
        
        
        
        
        <?php
// include footer
include 'includes/footer.php';
?>
        



            <script src="js/jquery-2.2.3.js"></script>
            <script type="text/javascript" src="js/bootstrap.min.js"></script>

    </body>
</html>
<?php
                                    }
                                }
                            ?>
<?php

include 'admin/dbconnect.php';

if(isset($_POST['submit'])){
    
    //comment variable dec. and init.
    $postId = $_GET['post_id'];
    $nameCom = mysql_real_escape_string($_POST['name']);
    $comment = mysql_real_escape_string($_POST['comment']);
    $dateCom = date("Y-m-d H:i:s");
   //insert into comment table
    $comm = "INSERT INTO green_comments (name,comment,post_id,date) VALUES('$nameCom','$comment','$postId','$dateCom')";
    $commSql = mysql_query($comm);
    
    if($commSql){
        
        header("Location :read_more.php");
        
    }else{
        echo 'Eno work o';
    }
    
}

?>
2
Contributors
5
Replies
15
Views
3 Weeks
Discussion Span
Last Post by Dani
2

In comment.php, you are retrieving Post ID via $_GET[] as opposed to via $_POST[]. GET is for query strings, such as comment.php?post_id=123 while POST is for data submitted from a form such as this one.

<form method="POST" name="form" action="comment.php">

    <div class="form-group">
        <input class="form-control" type="text" id="name" name="name" placeholder="Your name..." />
    </div>
    <div class="form-group">
        <textarea name="comment" id="message" class="form-control" placeholder="Your comment..."></textarea>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-success" name="submit">Comment</button>
    </div>

</form>

You can see here that you are passing in name and comment via POST, but you are not passing in post ID anywhere. You need to pass post ID into the form as well, preferably as a hidden input variable.

<input type="hidden" name="post_id" value="<?php echo $_GET['post_id']?>">
0

Thanks Uncle Dani....it worked as i inserted the hidden input variable... #HeartYA bro

0

Sir, one more thing.... if You look at my COMMENT.PHP page.. you'll see that i want the COMMENT.PHP page to redirect back to the READ_MORE.PHP page that carries the particular post that was just commented on.... so i used

header("Location :read_more.php");.... but its not working. its not even redirecting... it just stays in the blank COMMENT page... pls help

0

You cannot redirect to a different page if any HTML has already been echo'ed out to the page.

This probably isn't it, but try header("Location: read_more.php"); instead (notice the colon followed by a space).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.