简体   繁体   中英

Clicking on 'submit' button navigates to another page

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.

On the other hand, posting comment functionality doesn't work at all when I simply run the PHP file in a web browser. That means running the site in my directory root (like this: file:///C:/xampp/htdocs/jquery/main.php). Why so?

Last but not least, error messages are not shown in the page where the comment box is located, rather it prints the message in the first HOME page, at the top, above the header. For example I added a reminder ("Please fill all the fields") to user if they don't fill all the fields. This message gets printed in the starting page on my site.

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="main.php" 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-->

First off I would comment, but do not have enough reputation. As far as the redirect is concerned, I am assuming main.php is your home page and you have set:

<form action="main.php">

Meaning that when you submit the form successfully the action you have designated is 'main.php', if main.php is your home page then you are basically saying take me my home page on submit. If you leave action empty it should remain on the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM