Reputation: 39
Hey guys this is my first PHP project so I'm a rookie and I'm developing a forum website but I need to develop comment system here so I'm using the following code to accept the values from my HTML form which then assigns these values to two PHP variables and they call a function which I have stored in a different PHP file.
Now the problem is that these values are being submitted automatically whenever I refresh my webpage so my database is getting filled up with the same values that I assigned to those variables so I need your help in sorting this out.
<html>
<head>
<?php include 'menu.php'?>
<?php include 'include/core.php'?>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
$nme = trim($_POST['name']);
$cmnt = trim($_POST['comments']);
if(!empty($nme) && !empty($cmnt))
{
create_commentsystem($nme,$cmnt);
unset($nme);
unset($cmnt);
}
else
{
echo "Please enter the complete details";
unset($nme);
unset($cmnt);
}
}
?>
<link href="css/main.css" rel="stylesheet" type="text/css">
<link href="css/auth.css" rel="stylesheet" type="text/css">
<title>Doctors Forum</title>
</head>
<body>
<div id="container">
<div class="tab" align="Center">
Page Content Here
</div>
<div class="content">
<form method="post" name="form1" action="">
<input class="field" type="text" name="name" placeholder="Name" style="width:635px; height:40px;"/></br></br>
<textarea class="field" name="comments" placeholder="Leave Comments Here..." style="width:635px; height:100px;"></textarea></br></br>
<input class="btn" type="submit" value="Post" style="width:150px;" >
</form>
</div>
<div id="" class="tab" align="center">
<div>
</div>
</body>
</html>'
And here is the Function that is being called when I press the button and the problem is the same record is being inserted exactly twice every time i reload the page ` function create_commentsystem($name,$comments)
{
$name = $_POST['name'];
$comments = $_POST['comments'];
$conn = dbConnect();
mysqli_query($conn, "INSERT INTO comments(name, comments) VALUES('$name','$comments')");
$result = mysqli_query($conn, "SELECT * FROM comments ORDER BY id ASC");
while($row=mysqli_fetch_array($result))
{
echo "<div class='comments_content'>";
echo "<h4><a href='delete_commentsystem()?id=" . $row['id'] . "'> X</a> </h4>";
echo "<h1>" . $row['name'] . "</h1>";
echo "<h2>" . $row['comments'] . "</h2></br></br>";
echo "<h3>" . $row['date_publish'] . "</h3>";
echo "</div>";
}
$conn->close();
}`
Upvotes: 0
Views: 2009
Reputation: 211
You could try appending
'?submit=true'
to your action url. Use
$_GET['submit']
to access the value of 'submit'. Your code could look like this
if($_GET['submit'] == true):
#form processing code
$_GET['submit'] = false;
header('your original file');
endif;
Upvotes: 0
Reputation: 134
When you refresh the web page the browser sends the request again. The easiest solution is after saving the comment to use header to redirect.
Example:
if (add_comment_success()){
header('Location: http://www.current.url.com/');
exit();
}
Upvotes: 1
Reputation: 714
Two things can be done:
There is no other way of preventing this.
Upvotes: 0