0

I am about to build a comment section in my website. I made comment box. Posting comment and updating that into database works fine. But the problem is while submitting any comment it navigates me to the first page (in my case it's home page under the HOME tab). It doesn't let user stay at the same page.

main.php:

<!--php code for comment section starts--> 

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("comment_section");
    $name=isset($_POST['name'])? $_POST['name'] : '';
    $comment=isset($_POST['comment'])?$_POST['comment'] : '';
    $submit=isset($_POST['submit'])?$_POST['submit'] : '';

    $dbLink = mysql_connect("localhost","root","");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

    if($submit) {
        if($name&&$comment) {
            $insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
        } else {
            echo "please fill out all fields";
        }
    }
?>

<!--php code for comment section ends--> 
<!--building a comment section starts-->

<center>
    <form action="" method="POST">
        <table>
            <tr><td>Name: <br><input type="text" name="name"/></td></tr>
            <tr><td colspan="2">Comment: </td></tr>
            <tr><td colspan="5"><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
            <tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
        </table>
    </form>

<!--building a comment section ends-->
<!--building a comment section's functionality starts-->
<?php
    {
        $dbLink = mysql_connect("localhost","root","");
        mysql_query("SET character_set_results=utf8", $dbLink);
        mb_language('uni');
        mb_internal_encoding('UTF-8');

        $getquery=mysql_query("SELECT * FROM comment ORDER BY id DESC");
        while($rows=mysql_fetch_assoc($getquery)) {
            $id=$rows['id'];
            $name=$rows['name'];
            $comment=$rows['comment'];

            echo $name.'<br />'.'<br />'.$comment .'<br/>'.'<br/>'.'<hr size="1"/>';
        }    
    }    
?>
<!--building a comment section's functionality ends-->
zajonc
  • 1,935
  • 5
  • 20
  • 25

3 Answers3

1

Try putting <form action="main.php" method="POST"> instead of <form action="" method="POST">. Also, your code is vulnerable to SQL injection. See this StackOverflow discussion to see what I mean and how to prevent it.

Community
  • 1
  • 1
Edward
  • 456
  • 3
  • 8
0

Is this comments box is on home page or at any left , right panel.

If it is try to put 'the file path' into < form action 'Give ur file name' > And put ur comment insertion code into that page . Means a page where u want to stay user.

Jigisha Variya
  • 207
  • 1
  • 8
  • the comment box isn't in the Home page, its in another page. But I wrote all the code in one file (inside main.php). That's why I don't have any other file. – Jabir AL Fatah Aug 27 '14 at 09:46
  • Add header('Location: your filename.php');exit; where u want's to redirect user after insert query.. – Jigisha Variya Aug 27 '14 at 10:08
  • PHP Developer, could you please me clarify me a bit more? You mean that I sould add a header tag? – Jabir AL Fatah Aug 27 '14 at 10:18
  • PHP Developer, I tried, it shouldn't work. Because I didn't create any separate file for different Tabs. All the pages are written in one (main.php) file. My comment box is in my ABOUT ME page. After inserting comment it redirects me to my HOME page. It may be because both of my HOME and ABOUT ME pages are written under one same file. – Jabir AL Fatah Aug 27 '14 at 10:31
  • POST me here url here how u redirect to 'ABOUT ME ' Page. Did u use hash linking ? Means URL OF 'ABOUT ME' Page – Jigisha Variya Aug 27 '14 at 10:34
  • I didnt use hash linking. This JS code I used to redirect me to any page. $(document).ready(function(){ $('#all_contents>div').filter(':first').show(); $('ul>li>a').click(function () { $('#all_contents>div').hide(); var $this = $(this); var target = $this.attr('href'); $(target).show(); return false; }); }); – Jabir AL Fatah Aug 27 '14 at 10:39
  • PUT(# hashid after filename) into header("location:your filename.php#who_im");exit; – Jigisha Variya Aug 27 '14 at 10:45
  • No doesn't make any change. I did it like this: if($submit) { if($name&&$comment) { $insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') "); header("location:main.php#who_im"); exit; } else { echo "please fill out all fields"; } } – Jabir AL Fatah Aug 27 '14 at 10:56
  • Do u have only one page in ur site? In which file u put this section ? – Jigisha Variya Aug 27 '14 at 10:59
  • I put this in main.php file. I have few pages in my site, but Only one .PHP class and a .JS class where I wrote all the codes. In PHP file I wrote HTML and PHP code, in JS file I wrote javascript code. – Jabir AL Fatah Aug 27 '14 at 11:03
  • Then it should done with hask linking url only . Because u r navigate all page with '#id'. Try to use same url which r coming when u click on 'About Me'. check in browser and put same url in header(). – Jigisha Variya Aug 27 '14 at 11:07
  • About me has the same url as other page has. It shows the same for all pages in my browser: localhost/jquery/main.php – Jabir AL Fatah Aug 27 '14 at 11:21
  • Yes , But u put (#who_im)
  • WHO I'M
  • it means ur url will be http://localhost/jquery/main.php#who_im ( put like this) header("location:http://localhost/jquery/main.php#who_im"); – Jigisha Variya Aug 27 '14 at 11:27
  • I really appreciate your help. But unfortunately none of them is working. I tried in all the way you have instruted but they don't make any change. – Jabir AL Fatah Aug 27 '14 at 11:45
  • Sorry, now i need ur whole source code to solve this problem. Not getting why it's not working. – Jigisha Variya Aug 27 '14 at 11:47
  • could you please write your email here? I will be grateful to you! – Jabir AL Fatah Aug 27 '14 at 11:55
  • i tried with this : header("location:#who_im");exit; and it works fine . – Jigisha Variya Aug 29 '14 at 06:18