2012-06-05 63 views
3

我試圖使用Securimage PHP腳本(比較谷歌)爲驗證碼,但我有一個問題:當我按鏈接「不同的圖像」(顯示另一個驗證碼),我失去了所有的在我的PHP表單的字段中輸入的文本。因此,用戶將不得不再次重新輸入所有內容,這是有問題的。驗證碼以PHP格式刷新

這是代碼。 注意:我拿走了'#'作爲href,因爲出於某種原因,它無法更改驗證碼圖像。有誰知道爲什麼? (這發生在Safari和火狐)

<img id='captcha' src='/myproject/securimage/securimage_show.php' alt='CAPTCHA Image' /><br> <a href='' onclick='document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false'> [Different Image ]</a> 
+1

你可以發佈你的表單代碼的休息嗎? – Adi

+0

發生這種情況是因爲頁面刷新了,對不對? – Leri

+1

更好地嘗試谷歌的recaptcha –

回答

0

因爲你是從HREF這就是刪除「#」爲什麼整個頁面被刷新鏈接被點擊和各個領域的數據都將丟失時。不要刪除它,然後使用以下代碼

<img id='captcha' src='/myproject/securimage/securimage_show.php' alt='CAPTCHA Image' /> 
<br> 
<a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false;"> [Different Image ]</a> 
0

這不是因爲刪除'#'。由於您在<a>標記中使用了錯誤的引號,頁面會重新加載。當使用單引號'作爲html屬性以及onclick中包含的JavaScript時,瀏覽器將僅使用document.getElementById(作爲onclick事件。 return false;將不會被執行並且頁面將被重新加載。

對於html屬性,您總是可以使用",對於javascript可以使用'。或者在\的範圍內逃脫同樣的引號。

這也將工作,沒有 '#':

<a href="" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false;"> [Different Image ]</a> 

或者它的轉義版本:

<a href='' onclick='document.getElementById(\'captcha\').src = \'/securimage/securimage_show.php?\' + Math.random(); return false;'> [Different Image ]</a>