473,287 Members | 1,904 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,287 software developers and data experts.

New record saved when Refresh clicked

I have a working PHP/MySQL application used for data entry. The data
entry screen includes a "Save" button. The PHP code for this button
looks like this:

if (isset($_POST['cmdSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
include ("InsertRecord.inc"); // Insert new record
}
else
{
include ("UpdateRecord.inc"); // Update existing record
}
}

This works great. If the State File Number (primary ID) field is
blank, a new record is added and a new State File Number assigned,
otherwise the existing record is updated. However, if the user enters
some data and clicks "Save" to insert a new record and then clicks the
browser (IE) Refresh button, the insert logic is repeated and another
record is added to the database. Apparently, the cmdSave button
remains set and pressing Refresh executes the "InsertRecord" logic
again. The "Post" version of State File Number also remains blank
even though it exists on the screen after "Save".

I have tried quite a number of fixes for this problem, none of which
have worked satisfactorily. What I would like to do is reset the
cmdSave button after the InsertRecord logic executes. Is that
possible?

Alternately, if I could sense that the Refresh button had been clicked
rather than cmdSave, I could also prevent re-execution.

Register_globals is off.

Any suggestions are greatly appreciated.
Jul 17 '05 #1
9 4891

Uzytkownik "Mark" <hi***********@yahoo.com> napisal w wiadomosci
news:5e**************************@posting.google.c om...
I have a working PHP/MySQL application used for data entry. The data
entry screen includes a "Save" button. The PHP code for this button
looks like this:

if (isset($_POST['cmdSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
include ("InsertRecord.inc"); // Insert new record
}
else
{
include ("UpdateRecord.inc"); // Update existing record
}
}

This works great. If the State File Number (primary ID) field is
blank, a new record is added and a new State File Number assigned,
otherwise the existing record is updated. However, if the user enters
some data and clicks "Save" to insert a new record and then clicks the
browser (IE) Refresh button, the insert logic is repeated and another
record is added to the database. Apparently, the cmdSave button
remains set and pressing Refresh executes the "InsertRecord" logic
again. The "Post" version of State File Number also remains blank
even though it exists on the screen after "Save".

I have tried quite a number of fixes for this problem, none of which
have worked satisfactorily. What I would like to do is reset the
cmdSave button after the InsertRecord logic executes. Is that
possible?

Alternately, if I could sense that the Refresh button had been clicked
rather than cmdSave, I could also prevent re-execution.

Register_globals is off.

Any suggestions are greatly appreciated.


Its because when user clicks refresh is posting form post data again.
Force reload page after manipulate with all post data like this:

if (isset($_POST['cmdSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
include ("InsertRecord.inc"); // Insert new record
}
else
{
include ("UpdateRecord.inc"); // Update existing record
}
Header("Location: {$_SERVER['REQUEST_URI']}");
die;
}

It will reload your page and clean up all post data - when user will click
refresh button
it will post empty data so your "if (isset($_POST['cmdSave']))" won't catch
it.

Drizzt
Jul 17 '05 #2
"Mark" <hi***********@yahoo.com> wrote in message news:5e**************************@posting.google.c om...
I have a working PHP/MySQL application used for data entry. The data
entry screen includes a "Save" button. The PHP code for this button
looks like this:

if (isset($_POST['cmdSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
include ("InsertRecord.inc"); // Insert new record
}
else
{
include ("UpdateRecord.inc"); // Update existing record
}
}

This works great. If the State File Number (primary ID) field is
blank, a new record is added and a new State File Number assigned,
otherwise the existing record is updated. However, if the user enters
some data and clicks "Save" to insert a new record and then clicks the
browser (IE) Refresh button, the insert logic is repeated and another
record is added to the database. Apparently, the cmdSave button
remains set and pressing Refresh executes the "InsertRecord" logic
again. The "Post" version of State File Number also remains blank
even though it exists on the screen after "Save".

I have tried quite a number of fixes for this problem, none of which
have worked satisfactorily. What I would like to do is reset the
cmdSave button after the InsertRecord logic executes. Is that
possible?

Alternately, if I could sense that the Refresh button had been clicked
rather than cmdSave, I could also prevent re-execution.

Register_globals is off.

Any suggestions are greatly appreciated.


One way to solve this problem is to redirect the browser to a new page
using header() function after the submission.
Jul 17 '05 #3
More elegant:

Place a hidden field with a random value.

In the script called after submit the form, place a SELECT before the INSERT
to check if this random value exists.

If so, return a message saiyng something like 'the data is already stored'
oh, simply, don't do anything.

En las nuevas, Mark escribió:
I have a working PHP/MySQL application used for data entry. The data
entry screen includes a "Save" button. The PHP code for this button
looks like this:

if (isset($_POST['cmdSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
include ("InsertRecord.inc"); // Insert new record
}
else
{
include ("UpdateRecord.inc"); // Update existing record
}
}

This works great. If the State File Number (primary ID) field is
blank, a new record is added and a new State File Number assigned,
otherwise the existing record is updated. However, if the user enters
some data and clicks "Save" to insert a new record and then clicks the
browser (IE) Refresh button, the insert logic is repeated and another
record is added to the database. Apparently, the cmdSave button
remains set and pressing Refresh executes the "InsertRecord" logic
again. The "Post" version of State File Number also remains blank
even though it exists on the screen after "Save".

I have tried quite a number of fixes for this problem, none of which
have worked satisfactorily. What I would like to do is reset the
cmdSave button after the InsertRecord logic executes. Is that
possible?

Alternately, if I could sense that the Refresh button had been clicked
rather than cmdSave, I could also prevent re-execution.

Register_globals is off.

Any suggestions are greatly appreciated.

Jul 17 '05 #4
Arg
I use a seperate save script that, after saving the data, uses javascript to
redirect back to the form or wherever it needs to go.

// inputform.php
<form method=post action=saveit.php>
Data 1: <input type=data1>
<input type=submit>
</form>
// saveit.php
// search records for match
// update if match, new record if not
<script language="JavaScript">
alert('Data has been updated');
location.href = 'inputform.php';
</script>
"Mark" <hi***********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
I have a working PHP/MySQL application used for data entry. The data
entry screen includes a "Save" button. The PHP code for this button
looks like this:

if (isset($_POST['cmdSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
include ("InsertRecord.inc"); // Insert new record
}
else
{
include ("UpdateRecord.inc"); // Update existing record
}
}

This works great. If the State File Number (primary ID) field is
blank, a new record is added and a new State File Number assigned,
otherwise the existing record is updated. However, if the user enters
some data and clicks "Save" to insert a new record and then clicks the
browser (IE) Refresh button, the insert logic is repeated and another
record is added to the database. Apparently, the cmdSave button
remains set and pressing Refresh executes the "InsertRecord" logic
again. The "Post" version of State File Number also remains blank
even though it exists on the screen after "Save".

I have tried quite a number of fixes for this problem, none of which
have worked satisfactorily. What I would like to do is reset the
cmdSave button after the InsertRecord logic executes. Is that
possible?

Alternately, if I could sense that the Refresh button had been clicked
rather than cmdSave, I could also prevent re-execution.

Register_globals is off.

Any suggestions are greatly appreciated.

Jul 17 '05 #5
Arg
Of course then any back-button click will cause it to resubmit the data, you
could send it to a self closing pop-up window or use a webbug like below.

// getdata.php
<script language="JavaScript">
function setMe(){
var d1=document.getElementById('d1').value;
// I would probably add some form verification here
document.getElementById('dot').src='saveit.php?d1= '+d1;
alert('Data Saved');
document.getElementById('d1').value='';
}
</script>
Data 1 <input type=text id=d1><br>
<input type=button value='Save' onClick="setMe();">
<img id=dot src='dot.png' style='display:none'>

// saveit.php
<?php
// Put your data verification and save here
Header("Content-type: image/png");
$image = ImageCreate(5,5);
$bg = ImageColorAllocate($image,0,150,0);
imagefilledrectangle($image, 0,0, 5,5, $bg);
ImagePNG($image,'',100);
ImageDestroy($image);
?>

You have to have gd library active in your php.ini
The image color or size does not really matter as it is hidden by the
style='display:none'.
You could use the image to signal the save if you wanted to, I have used a
yellow image before the save and then changed it to green to show it was
saved.
"Arg" <r_***@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
I use a seperate save script that, after saving the data, uses javascript
to redirect back to the form or wherever it needs to go.

// inputform.php
<form method=post action=saveit.php>
Data 1: <input type=data1>
<input type=submit>
</form>
// saveit.php
// search records for match
// update if match, new record if not
<script language="JavaScript">
alert('Data has been updated');
location.href = 'inputform.php';
</script>
"Mark" <hi***********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
I have a working PHP/MySQL application used for data entry. The data
entry screen includes a "Save" button. The PHP code for this button
looks like this:

if (isset($_POST['cmdSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
include ("InsertRecord.inc"); // Insert new record
}
else
{
include ("UpdateRecord.inc"); // Update existing record
}
}

This works great. If the State File Number (primary ID) field is
blank, a new record is added and a new State File Number assigned,
otherwise the existing record is updated. However, if the user enters
some data and clicks "Save" to insert a new record and then clicks the
browser (IE) Refresh button, the insert logic is repeated and another
record is added to the database. Apparently, the cmdSave button
remains set and pressing Refresh executes the "InsertRecord" logic
again. The "Post" version of State File Number also remains blank
even though it exists on the screen after "Save".

I have tried quite a number of fixes for this problem, none of which
have worked satisfactorily. What I would like to do is reset the
cmdSave button after the InsertRecord logic executes. Is that
possible?

Alternately, if I could sense that the Refresh button had been clicked
rather than cmdSave, I could also prevent re-execution.

Register_globals is off.

Any suggestions are greatly appreciated.


Jul 17 '05 #6
Thanks for all the helpful suggestions. The problem with the redirect
(Header) idea is that it clears all the data from the screen. What I
want is for the data to remain on the screen so the user can continue
with entry. There is a great deal of data (12 screens), so it is
reasonable to expect the user to save several times during the entry
of a record.

As it currently exists, my InsertRecord.inc logic contains some code
that rereads the record after the Insert:

// Reread the record after the POST operation
// Fields on form are refreshed after the reread via HTML "Value"
clauses

$sql = "SELECT * FROM rosemaster
WHERE StateFileNumber = $NewSFN";

$result = mysql_query($sql) or die ("Failed rereading after insert");

$row = mysql_fetch_array($result); // Extract the record

This repopulates the screen and lets the user continue entering more
data. The next time "Save" is clicked, the logic senses the existence
of a State File Number and the Update logic is called instead on
Insert. Works great until the Refresh button comes into play.

I have tried using session variables to indicate that a record has
been inserted, but haven't got it to work quite right.

Is there a way to detect that the Refresh button has been clicked?
That seems to me to be the best solution.

Thanks for all the help!
Jul 17 '05 #7

"Mark" <hi***********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
Thanks for all the helpful suggestions. The problem with the redirect
(Header) idea is that it clears all the data from the screen. What I
want is for the data to remain on the screen so the user can continue
with entry. There is a great deal of data (12 screens), so it is
reasonable to expect the user to save several times during the entry
of a record.

As it currently exists, my InsertRecord.inc logic contains some code
that rereads the record after the Insert:

// Reread the record after the POST operation
// Fields on form are refreshed after the reread via HTML "Value"
clauses

$sql = "SELECT * FROM rosemaster
WHERE StateFileNumber = $NewSFN";

$result = mysql_query($sql) or die ("Failed rereading after insert");

$row = mysql_fetch_array($result); // Extract the record

This repopulates the screen and lets the user continue entering more
data. The next time "Save" is clicked, the logic senses the existence
of a State File Number and the Update logic is called instead on
Insert. Works great until the Refresh button comes into play.

I have tried using session variables to indicate that a record has
been inserted, but haven't got it to work quite right.
In what way is it *not working right*?
Is there a way to detect that the Refresh button has been clicked?
That seems to me to be the best solution.


There is nothing in the HTTP protocol which detects the REFRESH button being
pressed. This is a browser function which simply redoes the last action
whatever it was.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #8
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message news:<ch*******************@news.demon.co.uk>...
"Mark" <hi***********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
Thanks for all the helpful suggestions. The problem with the redirect
(Header) idea is that it clears all the data from the screen. What I
want is for the data to remain on the screen so the user can continue
with entry. There is a great deal of data (12 screens), so it is
reasonable to expect the user to save several times during the entry
of a record.

As it currently exists, my InsertRecord.inc logic contains some code
that rereads the record after the Insert:

// Reread the record after the POST operation
// Fields on form are refreshed after the reread via HTML "Value"
clauses

$sql = "SELECT * FROM rosemaster
WHERE StateFileNumber = $NewSFN";

$result = mysql_query($sql) or die ("Failed rereading after insert");

$row = mysql_fetch_array($result); // Extract the record

This repopulates the screen and lets the user continue entering more
data. The next time "Save" is clicked, the logic senses the existence
of a State File Number and the Update logic is called instead on
Insert. Works great until the Refresh button comes into play.

I have tried using session variables to indicate that a record has
been inserted, but haven't got it to work quite right.


In what way is it *not working right*?
Is there a way to detect that the Refresh button has been clicked?
That seems to me to be the best solution.


There is nothing in the HTTP protocol which detects the REFRESH button being
pressed. This is a browser function which simply redoes the last action
whatever it was.


Thanks, Tony. I'll try to explain the problem. I introduced a
session variable that contains the SFN (State File Number - unique
key) of a new record immediately after INSERT. It now looks like
this:

if (isset($_POST['cmdSubmitSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
if ($_SESSION['RefreshSFN'] <> "")
{
// Reread using Session Var

$TempSFN = $_SESSION['RefreshSFN'];

$sql = "SELECT * FROM rosemaster
WHERE StateFileNumber = $TempSFN";

$result = mysql_query($sql) or die ("Failed rereading after
refresh");

$row = mysql_fetch_array($result); // Extract the record

}
else
{
include ("InsertRecord.inc"); // Add a new record
$_SESSION['RefreshSFN'] = $NewSFN; // Store for reread if Refresh
clicked
}
}
else
{
include ("UpdateRecord.inc"); // Update existing record
$_SESSION['RefreshSFN'] = ""; // Clear
}
}

This works great. If the user clicks "Save" to INSERT a new record
and then clicks Refresh, the existing record is reread and returned to
the screen with no additional copy saved.

However, the program also includes a "New" button that clears the data
from the screen. This is a Normal button, not Submit. All it does is
use VBScript to clear the data from the textboxes.

If the user enters a record and clicks "Save", the record is INSERTed
and the session var loaded with the SFN. If the user then clicks
"New", the data are cleared from the screen, but the session var
remains loaded, so that when a new record is entered and "Save"
clicked, the session var exists and the previous record returned to
the screen. Not good.

I tried to find a way to clear the session var when "New" is clicked,
but haven't found a way yet since it is not a "Submit" button. Should
I try changing it to "Submit" and add PHP code to clear the session
var? Or is there a better approach?

Thanks again!
Jul 17 '05 #9

"Mark" <hi***********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:<ch*******************@news.demon.co.uk>...
"Mark" <hi***********@yahoo.com> wrote in message
news:5e**************************@posting.google.c om...
> Thanks for all the helpful suggestions. The problem with the redirect
> (Header) idea is that it clears all the data from the screen. What I
> want is for the data to remain on the screen so the user can continue
> with entry. There is a great deal of data (12 screens), so it is
> reasonable to expect the user to save several times during the entry
> of a record.
>
> As it currently exists, my InsertRecord.inc logic contains some code
> that rereads the record after the Insert:
>
> // Reread the record after the POST operation
> // Fields on form are refreshed after the reread via HTML "Value"
> clauses
>
> $sql = "SELECT * FROM rosemaster
> WHERE StateFileNumber = $NewSFN";
>
> $result = mysql_query($sql) or die ("Failed rereading after insert");
>
> $row = mysql_fetch_array($result); // Extract the record
>
> This repopulates the screen and lets the user continue entering more
> data. The next time "Save" is clicked, the logic senses the existence
> of a State File Number and the Update logic is called instead on
> Insert. Works great until the Refresh button comes into play.
>
> I have tried using session variables to indicate that a record has
> been inserted, but haven't got it to work quite right.


In what way is it *not working right*?
> Is there a way to detect that the Refresh button has been clicked?
> That seems to me to be the best solution.


There is nothing in the HTTP protocol which detects the REFRESH button
being
pressed. This is a browser function which simply redoes the last action
whatever it was.


Thanks, Tony. I'll try to explain the problem. I introduced a
session variable that contains the SFN (State File Number - unique
key) of a new record immediately after INSERT. It now looks like
this:

if (isset($_POST['cmdSubmitSave']))
{
if ($_POST[txtStateFileNumber] == "")
{
if ($_SESSION['RefreshSFN'] <> "")
{
// Reread using Session Var

$TempSFN = $_SESSION['RefreshSFN'];

$sql = "SELECT * FROM rosemaster
WHERE StateFileNumber = $TempSFN";

$result = mysql_query($sql) or die ("Failed rereading after
refresh");

$row = mysql_fetch_array($result); // Extract the record

}
else
{
include ("InsertRecord.inc"); // Add a new record
$_SESSION['RefreshSFN'] = $NewSFN; // Store for reread if Refresh
clicked
}
}
else
{
include ("UpdateRecord.inc"); // Update existing record
$_SESSION['RefreshSFN'] = ""; // Clear
}
}

This works great. If the user clicks "Save" to INSERT a new record
and then clicks Refresh, the existing record is reread and returned to
the screen with no additional copy saved.

However, the program also includes a "New" button that clears the data
from the screen. This is a Normal button, not Submit. All it does is
use VBScript to clear the data from the textboxes.

If the user enters a record and clicks "Save", the record is INSERTed
and the session var loaded with the SFN. If the user then clicks
"New", the data are cleared from the screen, but the session var
remains loaded, so that when a new record is entered and "Save"
clicked, the session var exists and the previous record returned to
the screen. Not good.

I tried to find a way to clear the session var when "New" is clicked,
but haven't found a way yet since it is not a "Submit" button. Should
I try changing it to "Submit" and add PHP code to clear the session
var? Or is there a better approach?

Thanks again!


You cannot clear a session var by running vbscript/javascript on the client
as all session data is held on the server. You should have a CLEAR button
which POSTs back to the web server (and PHP) so that the server-side script
can clear the session data and send back an empty screen.

Another way would be not to bother with storing RefeshSFN on the server but
rely on the contents of txtStateFileNumber in the $_POST array. Your
existing NEW button should empty this variable so that your PHP script can
see whether to create a new record or update an exiting one.

I personally don't waste my time with any client-side scripting as it may be
turned off or incompatible with the client's browser.

HTH.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
7
by: todholt | last post by:
Hello, I am trying to bypass a form's automatic update, and instead call a stored procedure in sql server. I am using continuous forms. The problem I am having is with returning to the next...
2
by: swingingming | last post by:
Hi, If I have a primary key, say orderID, after I refreshed the form, how can i get to that record via VBA. Thanks.
22
by: Br | last post by:
First issue: When using ADPs you no longer have the ability to issue a me.refresh to save the current record on a form (the me.refresh does a requery in an ADP). We usually do this before...
0
by: sara | last post by:
I have a simple app, and I'm trying to add Orders. I have tblOrders and tblOrderDetails. First, the user selects the customer, then "orders", "new Order" The frmNewOrder simply assures the...
1
by: Catriona | last post by:
I am developing an Access application where users insert bill records for an electricity account by clicking on a new button. The required workflow is 1) New button clicked 2) New record appears...
5
by: dzulai | last post by:
i have a continuous subform and its AllowAdditons and AllowEdits property are set to false. A command button("Add") in my main form sets both properties to True, enabling the New Record to appear. My...
6
by: AppDev63 | last post by:
I've been struggling with a form. On the left side of the form I've got data from a table of invoices, and on the right side I've got a combo box with data from a table of payments. The user scrolls...
0
by: trixxnixon | last post by:
i have a form that is being designed to pend requests in a requests database. the pend form is opened from an update form used by an employee to enter updates. when the pend form is updated, the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.