Posts: 41
Name: Jabis Sevon
Location: Tampere, Finland
|
Quote:
Originally Posted by qammar
Well,
you should display the user msgs on one page. and there there should also be a form, from where users/visitors submit their comments/msgs.
on the 2nd page you will deal with the submitted data, and after successfuly adding it into DB, users/visitor will be return to the form page/page1.
that is the best choice, I suggest.
Regards,
qammar feroz
|
This would be the ideal situation, but if you for example, would like to post stuff in your index page and after submission see the results straight away (IE. Polls) you would need to post the stuff to the index page itself, sometimes.
The alternative of course is to use another page and use a header redirect to the original after validating/inputting the data, this would kill the obvious double-posting issue, but it's not always necessary, and would sometimes require even more code lines to achieve the same result as burying the code in the same page
Here's a sample solution, first codeblock to the very top of the file:
PHP Code:
<?php if(isset($_POST['submit'])) { foreach($_POST as $key => $value) { /* we'll just store the results to a cookie and after it do a header refresh to kill the double-posting issue */ setcookie($key, $value); } header("Location: index.php?showresults"); } else if(isset($_GET['showresults'])) { if(isset($_COOKIE)) { $toEcho = "<h2> Your results </h2>"; foreach($_COOKIE as $submission => $value) { $toEcho .= "<h5>".$submission." posted has value ".$value."</h5>"; } $toEcho .= "<p>End of results</p>"; } else { $toEcho = "<strong>No posted data saved or cookies disabled</strong>"; } } else { $toEcho = <<< EOD <form name="name" method="post" action="index.php"> ...inputfields <input type="button" id="submit" name="submit">Submit</button> EOD; } ?>
and after that your basic html body, to the point where you wish to display the form / result and then just do to display it:
PHP Code:
<?php echo $toEcho; ?>
Hope I didn't go over too far with this post, but it shows how to evade the doubleposting issue as well as keep track of your submitted data in a cookie, where you could access it later...
Hope it helps :>
PS. Oh and I use input type button instead of type submit, to fix a few issues with IE's enter-key submission issue, and firefox's submit type not posting with the form itself ($_POST['submit'] not always seen in the post-data, so you'd never get to the handling of the post)
Last edited by jabis; 10-23-2008 at 02:10 PM..
Reason: added ps
|