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

Refresh problem

Hi,
I have refresh problem.When user does refresh or clicks F5 then form variables again posting. It crates same a lot of inserts. I don't know How to solve it.
May 8 '07 #1
14 2048
ak1dnar
1,584 Expert 1GB
I think you are using a html form to submit the data and there might be a submit button.
Let's assume that submit button is like this.

[HTML]<input type="submit" name="sub" value="Send">[/HTML]

then from your php side use like this.

[PHP]if ($_POST['sub'])
{
// insert current coding inside this if block for getting values from html form and //inserting it to the database
}[/PHP]

php will execute only user cllicks the sub button .
May 8 '07 #2
I have already done it. My code sample;
Submit buton definition
<input type="submit" name="kaydet" value="Kaydet">

insertion side

if($_POST['kaydet'] )
{
// I'm taking POST variables and inserting to database
}
May 8 '07 #3
ak1dnar
1,584 Expert 1GB
Good for you. :D
May 8 '07 #4
Purple
404 Expert 256MB
Hi seralasu,

I am assuming you are still having a problem with this.. a suggestion is to use Javascript on the onClick event of your submit button and set a hidden field on your form and test that rather than the submit button - the onClick event will only fire when clicked - ie it does not fire on refresh / F5..

the following code demonstrates using a basic javascript alert..
[PHP]
echo "<form name=\"test\" action=\"".$_SERVER["PHP_SELF"]."\" method = \"post\">
<input type=\"submit\" name=\"kaydet\" value=\"Kaydet\" onClick=\"javascript:alert('here');\"></input></form>";
print_r($_POST);
[/PHP]
May 8 '07 #5
subash
33
Hi,

Please check the thread http://www.thescripts.com/forum/thread576526.html

Subash :)
May 8 '07 #6
Purple
404 Expert 256MB
Hi Subash,

I don't think the thread you referenced answers the initial issue - the problem experience is caused by hitting refresh or F5 on a form using a method of post - basically the form is submitted every time the form is refreshed, F5 is hit or the submit button is pressed..

Rgds Purple
May 8 '07 #7
pbmods
5,821 Expert 4TB
Easiest way I can think of to prevent this is to set up some unique keys in MySQL.

For example, suppose you had a table that looked something like this:

Expand|Select|Wrap|Line Numbers
  1. mysql> DESCRIBE `Data_Categories`;
  2. +------------+---------------------+------+-----+---------+----------------+
  3. | Field      | Type                | Null | Key | Default | Extra          |
  4. +------------+---------------------+------+-----+---------+----------------+
  5. | categoryid | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment | 
  6. | desc       | varchar(64)         | NO   |     |         |                | 
  7. +------------+---------------------+------+-----+---------+----------------+
  8.  
Obviously, it wouldn't be useful to have a bunch of duplicate categories (this is an example; bear with me). So we would want to add a unique constraint so that every category has a different name.

To set up a unique key:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `Data_Categories` ADD UNIQUE KEY `catName`(`desc`);
  2.  
The `catName` is just the name of your key and can be just about anything you want it to be. You put the name of the column(s) you want constrained inside the parenthesis.

Once the unique is in place, attempting to insert a duplicate row will fail:

Expand|Select|Wrap|Line Numbers
  1. mysql> INSERT INTO `Data_Categories` (`desc`) VALUES('Tester');
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. mysql> INSERT INTO `Data_Categories` (`desc`) VALUES('Tester');
  5. ERROR 1062 (23000): Duplicate entry 'Tester' for key 2
  6.  
[EDIT: Just to make things complicated, you can put a constraint on more than one column in the table. For example, if you used:

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `Data_Categories` ADD UNIQUE KEY `parentData`(`parent`, `desc`);
  2.  
(assuming that `Data_Categories` also had a `parent` field)

Then an INSERT would only fail if you tried to add a row with the same `parent` AND `desc` as an existing row, but would still allow the INSERT if only one of the fields matched.]
May 8 '07 #8
Hi,
Thanks for all replies. I will try them.
May 9 '07 #9
paja
1
To AJAXRAND (sorry - I could not post this as a reply to your comment)

It does not work. Your $_POST['sub'] will be still there (the form will be sent again with F5 - refresh).

Have a try to add at the end of your PHP script this:
echo "<PRE>POST :";
print_r($_POST);
echo "</PRE>";

Paja
Jun 30 '07 #10
kovik
1,044 Expert 1GB
I think you are using a html form to submit the data and there might be a submit button.
Let's assume that submit button is like this.

[HTML]<input type="submit" name="sub" value="Send">[/HTML]

then from your php side use like this.

[PHP]if ($_POST['sub'])
{
// insert current coding inside this if block for getting values from html form and //inserting it to the database
}[/PHP]
Whoa whoa whoa. NEVER use the submit button as the condition of submission. A form can be submitted without use of the submit button, and the condition will never be met (this is an IE bug, so we can't ignore it).

As for the problem of this post, it is hardly a problem at all. It's just the way that HTTP POST works. It exists on the page that was posted to, and refreshing will re-submit data no matter what the situation. Since your problem is that your submission causes more database entries, try these:

1) Check the database for that entry before adding it in.
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT `id` FROM `table` WHERE `var1` = '" . mysql_real_escape_string($_POST['var1']) . "';");
  2.  
  3. if(mysql_num_rows() == 0)
  4. {
  5.     // Perform insertion
  6. }
  7.  
2) Create a hidden field that is filled upon posting and check for it
Expand|Select|Wrap|Line Numbers
  1. if(!empty($_POST) && isset($_POST['alreadyposted'] && $_POST['alreadyposted'] == 0)
  2. {
  3.     $alreadyposted = true;
  4.     // Handle the post
  5. }
  6.  
  7. ....
  8.  
  9. <form>
  10.     <input type="hidden" name="alreadyposted" value="<?php echo isset($alreadyposted) ? 1 : 0; ?>" />
Jun 30 '07 #11
ak1dnar
1,584 Expert 1GB
I Think, I made a Bad mistake without reading seralasu's Problem well.
Sorry for that.

seralasu,
Do you prefer to redirect the Page after submitting the form to another location or to the same page.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $con = mysql_connect('localhost', 'root', 'dba') or die ("Could not connect to the Database");
  3. mysql_select_db('test', $con) or die (mysql_error());
  4.  
  5. if(isset ($_POST['insert']) && $_POST['productname'] != "" ){
  6. $STR_NAME = $_POST['productname'];
  7. $SQL_INSERT = "INSERT INTO products (p_name) VALUES ('$STR_NAME')";
  8. mysql_query($SQL_INSERT) or die(mysql_error());
  9. header('Location:'.$PHP_SELF.''); 
  10. // You Can redirect to Another Page Also 
  11. //header('Location: thankyou.php'); 
  12. }
  13. ?>
  14. <html>
  15. <body>
  16. <form action="
  17. <input name="productname" type="text">
  18. <input name="insert" type="submit">
  19. </form>
  20. </body>
  21. </html>
Jul 2 '07 #12
kovik
1,044 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. header('Location:'.$PHP_SELF.'');
*Ahem* Umm... I really didn't think there'd be need to contradict a mod twice in the same post. I hope it doesn't seem rude, but these are points of security and usability.

$SERVER['PHP_SELF'] and it's deprecated global counterpart, $PHP_SELF, are unsafe and inconsistent. They cause different outputs on CGI installations. It is unsafe because it openly allows XSS. An alternative to $_SERVER['PHP_SELF'], in the sense that most people use it for, is basename(__FILE__).

Also, header() is not meant to accept relative paths. Browsers are not required, by standards, to accept and translate relative paths through redirection. You must always give an absolute path when using header() redirection.
Jul 2 '07 #13
I believe the answer is here:
It uses sessions to hold a secret code and verifies on the form handle. It then creates a new secret code and overwrites the session so if the form is refreshed the two codes do not match.

php no refresh script
Jul 2 '07 #14
ak1dnar
1,584 Expert 1GB
*Ahem* Umm... I really didn't think there'd be need to contradict a mod twice in the same post. I hope it doesn't seem rude, but these are points of security and usability.

$SERVER['PHP_SELF'] and it's deprecated global counterpart, $PHP_SELF, are unsafe and inconsistent. They cause different outputs on CGI installations. It is unsafe because it openly allows XSS. An alternative to $_SERVER['PHP_SELF'], in the sense that most people use it for, is basename(__FILE__).

Also, header() is not meant to accept relative paths. Browsers are not required, by standards, to accept and translate relative paths through redirection. You must always give an absolute path when using header() redirection.
I really appreciate your feedback volectricity ! Thanks.

And it’s a good point to start a new thread for Good and Bad PHP Code here at PHP Forum.

Thanks,
-Ajaxrand
Jul 3 '07 #15

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

Similar topics

1
by: James | last post by:
I have a data grid refresh problem. I have a few columns and the first column is data in the form of numbers. And in the form of the data grid if I specify for example something like a code(in a...
4
by: wright | last post by:
hello, i have a problem in refreshing a page. the problem happens when i click a button that does a certain action in a web page, such as inserting a record into the database, if now i refresh the...
4
by: sandhya.net | last post by:
Hi, I have a dropdown which displays the Products and Add button on my Asp.net web page. When the user selects a product from the dropdown and Clicks the add button it adds the selected product...
6
by: Bernie Hunt | last post by:
I have a simple app that grabs records from a database and steps through them processing each record. I have three text fields on the form to give the user feedback on the progress. With each...
6
by: George | last post by:
Hi, I have been encountering a refresh problem with DataGridView, which is bound to a DataTable. When I make updates (Add, Delete, update) to the DataGridView, everything flow nicely to...
0
by: khurram.shakir | last post by:
I am developing an application, which uses .NET 2.0/WinForms and has a designer for screen layout designing. User has an option to design the layout of screen, and for that we developed our own...
2
by: shekharanjali | last post by:
hi, i have a page refresh problem , when the page is refreshed the last query or the command gets executed, is this problem something to do with cookies.please reply. thanks
1
jackb
by: jackb | last post by:
Hello there. I have a webpage that need to reload every 10 seconds, so I have put a meta tag: <meta http-equiv="refresh" content="10"> Now this seems to work ok. Now the problem is that my...
4
by: EManning | last post by:
Using A2003 w/ tables linked to SQL Server. All users have their own copy of the mdb. I have a combobox whose rowsource is a query. This query is based on a table and has a field in it that...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.