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

New record saved when Refresh clicked

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a

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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.