473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
7 14631
ak1dnar
1,584 Expert 1GB
Please use the [code=php] tags when posting source codes in future. Thanks.
Sep 3 '07 #2
code green
1,726 Expert 1GB
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
Anette
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
1,726 Expert 1GB
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
Anette
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
1,726 Expert 1GB
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
5,821 Expert 4TB
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

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

Similar topics

8
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have...
2
by: TR | last post by:
In an "Abandon Changes" button on my UserPreferences.aspx form, I'd like to redirect back to the page the user was on before coming to the UserPreferences form. It could be any one of my web app's...
3
by: NuB | last post by:
I have a C# web form that allows the user to perform a search if all the required fields are filled in, which works fine, my issue is if a user does a sucessfull search then enters in data and...
10
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
33
by: STILL LEARNING | last post by:
I'm not sure if this can even be done, but what prompts the question is my desire to be able to create an "Uber Link" script/code of some sort, such that even if the html page contains nothing but...
7
by: Xh | last post by:
Hi All, I have problems with generating valid HTML output there are few HTML elements that i don't what to output as <tagname/> but as <tagname></tagnamebut Xalan keeps generating them as...
1
Fary4u
by: Fary4u | last post by:
Hi Guys i'm trying to upload a file, i've tried 3 different methods but still not work out i don't know how to over come this problem hidden file value, multiple form or popup uploading. 1-...
0
Fary4u
by: Fary4u | last post by:
Hi Guys i'm trying to upload a file, i've tried 3 different methods but still not work out i don't know how to over come this problem hidden file value, multiple form or popup uploading. 1-...
3
by: Willy | last post by:
I have a website that uses querystrings on a certain page to show multiple contents. I have created mapped pages to hide the gory details of these querystrings. So instead of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.