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: - <html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
-
<title>PHP SQL Test
-
<link rel="stylesheet" href="inc/beyond.css" type="text/css">
-
</head>
-
<body bgcolor="#FFFFFF">
-
<?php include("insert.php"); ?>
-
<form method="POST" action="
-
Firstname: <input type="text" name="firstname" />
-
Lastname: <input type="text" name="lastname" />
-
Age: <input type="text" name="age" />
-
<input type="hidden" name="action" value="submitted" />
-
<input type="submit" name="submit" value="submit me!" />
-
</form>
-
<?php unset ($_SESSION['formSubmitted']); ?>
-
</body>
-
</html>
insert.php: - <?php
-
$db = mysql_connect("localhost", "rmweb_se", "jsh53hKT");
-
// The "die" part will be executed if the connection fails.
-
if (!$db) {
-
die('Could not connect: ' . mysql_error());
-
}
-
else
-
echo "Database was connected<br />";
-
mysql_select_db("rmweb_se", $db);
-
?>
-
<?php
-
if (isset($_POST['action']) && $_POST['action'] == 'submitted') {
-
echo '<pre>';
-
print_r($_POST);
-
echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again';
-
echo '</pre>';
-
$result = mysql_query("SELECT * FROM person");
-
echo "<table border='1'>
-
<tr>
-
<th>Firstname
-
<th>Lastname
-
</tr>";
-
while($row = mysql_fetch_array($result)) {
-
echo "<tr>";
-
echo "<td>" . $row['FirstName'] . "";
-
echo "<td>" . $row['LastName'] . "";
-
echo "</tr>";
-
}
-
echo "</table>";
-
}
-
?>
7 14631
Please use the [code=php] tags when posting source codes in future. Thanks.
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]
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=...>?
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]
...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 - <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
-
Firstname: <input type="text" name="firstname" />
-
Lastname: <input type="text" name="lastname" />
-
Age: <input type="text" name="age" />
-
Show Data: <input type="checkbox" name="showdata" value="10"/>
-
<input type="hidden" name="action" value="submitted" />
-
<input type="submit" name="submit" value="submit me!" />
-
</form>
and that I should have another main.html with - <html>
-
<head>
-
<title>PHP SQL Test</title>
-
<link rel="stylesheet" href="inc/beyond.css" type="text/css">
-
</head>
-
<body bgcolor="#FFFFFF">
-
<?php include("insert_new.php"); ?>
-
</body>
-
</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).
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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-...
|
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-...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |