Hi all,
I have a web page, in this page i included the php page by using the include().
But this php page is having a form with some fields and submit button…
After submitting of the page, i want to show the user submitted data in the same page.
But it is not working at first time, why?
When i submit the form again, it will display errors and now it is showing the user data…
I used to store it in the database first, and then i display it in the page…
for that i used, header(_) function to redirect to this page, that is also not showing at first time, why?
Is there any wrong with me…
Help regarding the redirection as well as the refreshing the page, to show the user submitted data…
You can submit the comment in this page, after clicking the ‘add comment’ button in this page, you can’t see the posted comment on this page for first time, and you submit the second time by clicking the ‘add comment’ button, it shows alert message with earlier submitted comment…
means, it is inserting the data in the database at first time, but it is not showing…
Here displaying page is including the comments page…
When i run this as separate page, by calling the name,it is showing the newly added comment…
but when i include it in another page, it is not showing the recently submitted one at first time, why?
This is example.php
<?php
//connect to your database
//query comments for this page of this article
$inf = "SELECT * FROM `test_table` WHERE page = '".stripslashes($_SERVER['REQUEST_URI'])."' ORDER BY time ASC";
$info = mysql_query($inf);
if(!$info) die(mysql_error());
$info_rows = mysql_num_rows($info);
if($info_rows > 0) {
echo '<h5>Comments:</h5>';
echo '<table width="95%">';
while($info2 = mysql_fetch_object($info)) {
echo '<tr>';
echo '<td>"'.stripslashes($info2->subject).'" by: <b>' .stripslashes($info2->username).'</b></td> <td><div align="right">'.date('h:i:s a', $info2->time).' on '.$info2->date.'</div></td>';
echo '</tr><tr>';
echo '<td colspan="2"> '.stripslashes($info2->comment).' </td>';
echo '</tr>';
echo '<tr><td colspan="2"><hr></td></tr>';
}//end while
echo '</table>';
//echo '<hr width="95%" noshade>';
} else echo 'No comments for this page. Feel free to be the first <br>';
if(isset($_POST['submit'])) {
$username1=addslashes($_POST['username']);
$contact=addslashes($_POST['contact']);
$subject=addslashes($_POST['subject']);
$comment=addslashes($_POST['comment']);
//try to prevent multiple posts and flooding...
$c = "SELECT * from `test_table` WHERE ip = '".$_SERVER['REMOTE_ADDR']."'";
$c2 = mysql_query($c);
while($c3 = mysql_fetch_object($c2)) {
$difference = time() - $c3->time;
if($difference < 300) $error.='<u><b>ALERT:</u> '.$c3->username.', You have already commented earlier; if you have a question, try the forums!</b><BR>';
} //end while
if($_POST['check'] == $_SESSION['check'])
{
//add comment
$q ="INSERT INTO `test_table` (article_id, page, date, time, username, ip, contact, subject, comment) VALUES ('".$_GET['id']."', '".$_POST['page']."', '".$_POST['date']."', '".$_POST['time']."', '".addslashes(htmlspecialchars($_POST['username']))."', '".$_SERVER['REMOTE_ADDR']."', '".addslashes(htmlspecialchars($_POST['contact']))."', '".addslashes(htmlspecialchars($_POST['subject']))."', '".addslashes(htmlspecialchars(nl2br($_POST['comment'])))."')";
$q2 = mysql_query($q);
if(!$q2) die(mysql_error());
//refresh page so they can see new comment
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_POST['page']);
}
}// else { //display form
?>
<legend>Post a comment</legend>
<div style="font-size:13px; color:#cc0000; margin-top:10px; margin-left:15px"><?php echo @$error; ?></div><br/>
<form name="comments" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="page" value="<?php echo($_SERVER['REQUEST_URI']); ?>">
<input type="hidden" name="date" value="<?php echo(date("F j, Y.")); ?>">
<input type="hidden" name="time" value="<?php echo(time()); ?>">
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="right">Name: </div></td>
<td><input name="username" type="text" size="30" value="<?php echo @$username1; ?>"></td>
</tr><tr><td></td></tr>
<tr>
<td><div align="right">Email: </div></td>
<td><input type="text" name="contact" size="30" value="<?php echo @$contact; ?>"></td>
</tr><tr><td></td></tr>
<td><div align="right">Subject: </div></td>
<td><input type="text" name="subject" size="30" value="<?php echo @$subject; ?>"></td>
</tr><tr><td></td></tr>
<tr>
<td><div align="right">Comment: </div></td>
<td><textarea name="comment" cols="45" rows="10" wrap="VIRTUAL"><?php echo @$comment; ?></textarea></td>
</tr>
<tr><td><div align="right"><img src="math_captchas.php"></div></td>
<td><input type="text" name="check" size="5" maxlength="2" ></td></tr>
<tr><td><br></td></tr>
<tr>
<td></td>
<td colspan="2"><input type="reset" value="Reset Fields">
<input type="submit" name="submit" value="Add Comment"></td>
</tr>
</table>
</form>
You select and display the comments from the database first, and then check if a new comment has been posted and add it to the database. Switch those two things around.
Thanks Mandes.
It appears, but not first time submission…
Thanks Guido2004;
You select and display the comments from the database first, and then check if a new comment has been posted and add it to the database. Switch those two things around.
Why do you have the redirect?
Am also doing this only…
First retrieving the comments from database, after submission…
or Can you explain how to do this…
I can’t get you…
In my code,
first selecting the data from database and displaying them…
and after submitting the form only, i am inserting the new one…
Thank you, i got it now…
But now another problem is;
‘reset fields’ button is not resetting the fields…
and after successful insertion, i want to remove the data in the text fields, if it is having any errors it will show upto the successful insertion…
The reset is working, the function of the reset is to return the field back to the default value, since you are writing the value field each time you output the form these are the default values.
Dont believe me ! Try changing one field then clicking reset, it will go back to your writen default value.
You only need to be entering these default values if there was an error with the data entered, so just add an if statement to check the validity of the entry.
If the entry was good you dont write the values, if the entry contained errors you do rewrite the values.
I explained the reset back in the previous post and told you how to sort the problem. The first time isnt a problem for you as you are writing blank data to the fields therefore the reset takes it back to blank. Reread my answer.
For the image, you cannot just bolt together pieces of code found from the internet and expect it all to work, you have to have some understanding of basic principles.
We would need to see the code your using for the image BUT since this thread is now offtopic I suggest you repost another thread about the verification image once youve sorted the reset problem