遊客:  註冊 | 登錄 | 幫助





標題: [求助] PHP Refresh 導致重覆 entry
  CoolQQ     Rank: 3
青出藍
性別 保密
UID 301

精華 0
帖子 247
積分 474   詳情

閱讀權限 40
註冊 2006-6-28
來自
狀態 離線

 
 
 
 
發表於 2007-7-7 05:40 AM  資料  個人空間  短訊  加為好友 
PHP Refresh 導致重覆 entry

而家學緊寫 php,

我做左一個 file submit form,  (其實咩form都唔重要)
當個form submit左, 就會留番o係同一頁, 等user可以再入另外既data + upload.
而且submit完, 會入呢個form既資料去 MySQL database 度.


如果submit完之後, click左 refresh / F5,
個page睇落無野,
但係就做多左一次upload 同埋加多左個entry 入去  MYSQL DB度~


我想問下, 點樣可以避免 click Refresh 攪到重覆 entry?


我睇過有人話靠 SESSION variables,
但係我唔係好知點做~~

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-7-7 10:08 AM  資料  個人空間  短訊  加為好友 
一般黎講﹐呢個係 webapp 既 nature﹐通常我地都唔會太刻意去避免。  但如果真係有需要﹐簡單黎講﹐您要做個 redirect﹐即係先 submit 去一頁 "中間站"﹐然後再響 PHP 入面 redirect 返去您果一頁。 詳細留俾下一位講﹐或者我遲 d 入黎再講﹐因為而家要出街。

頂部



lhy     Rank: 3
青出藍
性別 男
UID 14043

精華 0
帖子 121
積分 330   詳情

閱讀權限 40
註冊 2006-10-16
來自 Netherlands
狀態 離線

 
 
 
 
發表於 2007-7-7 10:15 PM  資料  個人空間  短訊  加為好友 
It is just like mickey said.. you cannot avoid this unless you are using AJAX. what you can do to avoid they refresh and do the same thing again is go to another page.... and redirect it back to your form. that is a posibility.

form.php

QUOTE:
<form src="submittedpage.php">
bla bla bla
</form>

submittedpage.php

QUOTE:
do the database thing and all other handles.
<script type="text/javascript">
<!--
window.location = "http://www.domain.com/form.php"
//-->
</script>

this way it will go to the submittedpage first and than put the user back to the form.php page. browser will now have no previous data of this form and will not do the `same thing` again when you hit refresh.

this is a really simple workaround I hope this will help you.

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-7-7 11:11 PM  資料  個人空間  短訊  加為好友 
Yup, the way lhy suggested is one way.  In PHP here is another way:

form.php

QUOTE:
<form src="submittedpage.php">
bla bla bla
</form>

submittedpage.php

QUOTE:
<?php
/* do the database thing and all other handles. */

header("Location: http://www.yourhost.com/form.php"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>


頂部

  CoolQQ     Rank: 3
青出藍
性別 保密
UID 301

精華 0
帖子 247
積分 474   詳情

閱讀權限 40
註冊 2006-6-28
來自
狀態 離線

 
 
 
 
發表於 2007-7-13 12:30 AM  資料  個人空間  短訊  加為好友 
明左少少 唔該晒,

但如果用戶 click "BACK" button
又會唔會變成 重覆entry 呢?

另外,

由 form.php 去 submitpage.php 既時候...
個 form D data 點樣傳去 submitpage 果度架?

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-7-13 01:17 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 CoolQQ 於 2007-7-12 12:30 PM 發表

但如果用戶 click "BACK" button
又會唔會變成 重覆entry 呢?

照理由係唔會既﹐click BACK 只會去返 browser history 裡面既前頁﹐所以有時如果果一頁既 cache expired 左﹐佢會顯示唔到。

QUOTE:
原帖由 CoolQQ 於 2007-7-12 12:30 PM 發表
由 form.php 去 submitpage.php 既時候...
個 form D data 點樣傳去 submitpage 果度架? ...

咦? 您係想知個中原理?  定係想知響 submitpage.php 點讀 d form data 呢?

CoolQQ :
哦, 諗到lu, 係呀, 其實係想知點樣寫者~~
係唔係咁:

<FORM action="submitpage.php" method="post">

咁樣整個form, d data就會send晒去 submit page,
rite?

今日做左成日, 都係自修中... (做到腦都大埋...)
php 都幾好玩喎~
都無諗到出到黎第一份工就係寫php kaka
我而家幫緊公司寫portal web,
要handle好多files 呀, data 呀之類~
之前掂都未掂過~~

好在公司俾我慢慢學, 慢慢吸收 :)

========================

另外想問番 BACK 同 history 問題:


即係話如果係用 header("http....") 去轉導個page,
個 browser 係唔會有 history 的嗎??

即係:

website 1: www.yahoo.com

website 2: abcde.php ---->submit 轉導到 somewhere

如果我由 somewhere果度 hit "BACK", 係唔會去左 submitform.php or abcde.php, 會番去 yahoo.com???

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-7-13 11:16 AM  資料  個人空間  短訊  加為好友 
Ohhh!  原來係工作上需要用 PHP 呀??  好播!!  寫 Web App 好玩呀!!!    有咩響度研究下丫!!  PHP 我都係學緊架咋!

QUOTE:
即係話如果係用 header("http....") 去轉導個page,
個 browser 係唔會有 history 的嗎??

即係:

website 1: www.yahoo.com

website 2: abcde.php ---->submit 轉導到 somewhere

如果我由 somewhere果度 hit "BACK", 係唔會去左 submitform.php or abcde.php, 會番去 yahoo.com???

以上例子﹐Click BACK 係會去返 www.yahoo.com 。  其實 新天藍 都係用呢個 redirect 既方法﹐所以您可以做下實驗。   viewthread.php 內既 快速回覆 --> submit 去 post.php --> redirect 去 viewthead.php。  您 Submit 左個 reply 後 再 click BACK﹐佢會去返您打 reply 果一頁﹐但其實中間係經過左 post.php 。  

CoolQQ :
哦, 原來係咁,
一陣去試帖度試下先,
唔該晒呀~ :)

頂部

lhy     Rank: 3
青出藍
性別 男
UID 14043

精華 0
帖子 121
積分 330   詳情

閱讀權限 40
註冊 2006-10-16
來自 Netherlands
狀態 離線

 
 
 
 
發表於 2007-7-14 08:42 PM  資料  個人空間  短訊  加為好友 
sending data from form.php to submitpage.php is done by post.

QUOTE:
<FORM action="submitpage.php" method="post">

there is 2 type of method you can use. Post or Get. by using post you are sending data in the background. and get method will put the data in the url.

In php if you want to receive data from post you can use the $_POST array.

little example:
form.php

QUOTE:
<FORM action="submitpage.php" method="post">
<input type="text" name="UserData" />
<input type="text" name="UserData2" />
</FORM>

submitpage.php

QUOTE:
<php
   $userdata = $_POST['UserData'];
   $userdata1 = $_POST['UserData1'];

?>

By using some kind of flags you can prevent some action will execute again when you visit a page again (back button or something. The best way is do that is with session vars.

CoolQQ :
好詳盡, 唔該晒~

其實之前都睇過,
但係因為初學, 所以一時間醒唔起~

俾你提一提我, 又重新記番晒~
印象深左~


唔該晒:)



最後編輯: lhy : 2007-7-14 08:48 PM
頂部


快速美言











一群熱心會員對本主題作出以下的回覆:

CoolQQ: RMK added - PID 119825 [ view ]
CoolQQ: RMK added - PID 119825 [ view ]
CoolQQ: RMK added - PID 119825 [ view ]
CoolQQ: RMK added - PID 119997 [ view ]
CoolQQ: RMK added - PID 120486 [ view ]



最後回覆日期: 2007-7-16 02:58 AM
  編輯帖子
快速美言
           


當前時區 GMT+8, 現在時間是 2024-4-25 04:06 PM

    Powered by Discuz!  © 2001-2007 Comsenz Inc.   
Processed in 0.741182 second(s), 9 queries

清除 Cookies - 聯繫我們 - LIPS Corner 新天藍 - Archiver