By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

Get output on same page as form. Reload page will "empty page".

P: 16
Hi,

I'm very new at PHP. I've tried for a long time now to get a page that has a form in a certain way:
1. When form is submitted the data in a table should be writted on the same page.

2. When I load and reload the page I want it to be lika a new session (only show the form not the data). As it is now isset($_POST['action'] and isset($_POST['submit'] is always set and the data is always shown. Except when a click the link "Please try again" (hich is a code I saw on a page), don't understand why...

php-fil:
Expand|Select|Wrap|Line Numbers
  1.  <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <title>PHP SQL Test
  5. <link rel="stylesheet" href="inc/beyond.css" type="text/css">
  6. </head>
  7. <body bgcolor="#FFFFFF">
  8.     <?php include("insert.php"); ?>
  9.     <form method="POST" action="
  10.     Firstname: <input type="text" name="firstname" />
  11.     Lastname: <input type="text" name="lastname" />
  12.     Age: <input type="text" name="age" />
  13.     <input type="hidden" name="action" value="submitted" />
  14.     <input type="submit" name="submit" value="submit me!" />
  15.     </form>
  16.     <?php unset ($_SESSION['formSubmitted']); ?>
  17. </body>
  18. </html>
insert.php:
Expand|Select|Wrap|Line Numbers
  1.      <?php 
  2.         $db = mysql_connect("localhost", "rmweb_se", "jsh53hKT");
  3.         // The "die" part will be executed if the connection fails.
  4.         if (!$db) {
  5.             die('Could not connect: ' . mysql_error());
  6.         }
  7.         else
  8.             echo "Database was connected<br />";
  9.         mysql_select_db("rmweb_se", $db);
  10.     ?>
  11.     <?php
  12. if (isset($_POST['action']) && $_POST['action'] == 'submitted') {
  13.     echo '<pre>';
  14.     print_r($_POST);
  15.     echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again';
  16.     echo '</pre>';
  17.             $result = mysql_query("SELECT * FROM person");
  18.             echo "<table border='1'>
  19.             <tr>
  20.             <th>Firstname
  21.             <th>Lastname
  22.             </tr>";
  23.             while($row = mysql_fetch_array($result)) {
  24.                 echo "<tr>";
  25.                 echo "<td>" . $row['FirstName'] . "";
  26.                 echo "<td>" . $row['LastName'] . "";
  27.                 echo "</tr>";
  28.             }
  29.             echo "</table>";
  30.         }
  31.     ?>
Sep 3 '07 #1
Share this Question
Share on Google+
7 Replies


ak1dnar
Expert 100+
P: 1,584
Please use the [code=php] tags when posting source codes in future. Thanks.
Sep 3 '07 #2

code green
Expert 100+
P: 1,726
As it is now isset($_POST['action'] and isset($_POST['submit'] is always set and the data is always shown. Except when a click the link "Please try again"
The $_POST array is passed when the form is submitted so all form objects are set (except unchecked checkboxes).
So the following will always evaluate to true [PHP](isset($_POST['action']) && $_POST['action'] == 'submitted') [/PHP] When you click the hyperlink, the form is bypassed so the $_POST array is not passed and the form objects are not set.
A simple way around is to empty the hidden variable when reloading the page, then check whether empty.
And check the submit button has been clicked
[PHP]if (isset($_POST['submit'])
{
if(empty($_POST['action']))
//do this
else
// do that[/PHP]
Sep 3 '07 #3

P: 16
The $_POST array is passed when the form is submitted so all form objects are set (except unchecked checkboxes).
But why does it show the data even on reload? I mean why is the $_POST array set even on reload? Shouldn't $_POST be reset/unset?

When you click the hyperlink, the form is bypassed so the $_POST array is not passed and the form objects are not set.
This is really what I don't get... I can see it works that way and that's what I want with a reload. Why is form bypassed?

A simple way around is to empty the hidden variable when reloading the page,
Where? Like <BODY load=...>?
Sep 4 '07 #4

code green
Expert 100+
P: 1,726
A hyperlink takes the user from their current page to the page in the href string.
The only variables passed are any URL variables attached to the string.
The browser treats the page as a completely new page even if it returns to the same page.

I have looked at your code and your requirements again and I can see why you are getting confused.
First it is very good that you have kept the HTML and PHP seperate but I much prefer having seperate files
ie. form.html and form.php. It is much tidier and easier to maintain
The form action should be form.php, then form.php starts with a simple test - has the form been submitted [PHP]if(isset($_POST['submit'])[/PHP]if it has write the data in the HTML table and update the database.
If not show the form [PHP]else
include 'form.html';[/PHP]Not sure what you mean by reload but you could [PHP]include 'form.html';[/PHP] at the end of form.php or echo a hyperlink to form.html in form.php [PHP]echo <a href="form.html">New Form</a>[/PHP]
Sep 4 '07 #5

P: 16
...seperate files ie. form.html and form.php.
What would be the mainfile? Do I understand right if only this is in the form.html

Expand|Select|Wrap|Line Numbers
  1. <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  2.         Firstname: <input type="text" name="firstname" />
  3.         Lastname: <input type="text" name="lastname" />
  4.         Age: <input type="text" name="age" />
  5.         Show Data: <input type="checkbox" name="showdata" value="10"/>  
  6.         <input type="hidden" name="action" value="submitted" />
  7.         <input type="submit" name="submit" value="submit me!" />
  8.     </form>
and that I should have another main.html with
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>PHP SQL Test</title>
  4. <link rel="stylesheet" href="inc/beyond.css" type="text/css">
  5. </head>
  6. <body bgcolor="#FFFFFF">
  7. <?php include("insert_new.php"); ?>
  8. </body>
  9. </html>
With reload I mean when user clicks the button Refresh (F5). I want the page to look like when it was loaded (i.e. only the form, no data from db).
Sep 9 '07 #6

code green
Expert 100+
P: 1,726
and that I should have another main.html
Well the page arrangement is entirely up to you. I simply suggested placing the HTML and PHP for the form in seperate files.
With reload I mean when user clicks the button Refresh (F5).
The refresh button is a browser tool. Calling it a reload completely threw me.
If I click the refresh button I would expect exactly the same page to re-appear.
Using this tool to load a new page with a blank form sounds insane.
Where have you seen this happen?
Sep 10 '07 #7

pbmods
Expert 5K+
P: 5,821
Heya, Anette.

Instead of outputting the table on the same page that interacts with the database, use header() redirection to redirect to a different page that actually displays the table. This way, the database script is no longer a part of the browsers history, so refreshing the page would have no effect.
Sep 10 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.