Ajax允許你發送一個請求到另一個頁面無需刷新。 檢查的jQuery的Perform an asynchronous HTTP (Ajax) request
在你的情況這部分,你設置了這樣的(1日,包括jQuery的在你的頁面的頭):
<p class="reg-code">Hash: <span></span>
<i class="fa fa-refresh" aria-hidden="true" id="refresh-code"></i></p>
注意,< SPAN>當我們啓動時,將處理響應爲空(但可以在開始時顯示原始哈希,當您點擊鏈接時它的內容將被替換),並且附有刷新鏈接的標識
然後,你使用jQuery:
$(document).ready(function() {
$("#refresh-code").click(function(){ // click on element with ID 'refresh-code'
$.ajax({ // method default is 'GET'
url: "hash.exe.php", // the PHP page that will generate new hash
success: function(html){ // if no PHP error 'html' is the response
$(".reg-code > span").html(html); // we append the html in the span
},
error: function (request, status, error) { // we handle error
alert(request.responseText);
}
});
});
});
PHP(hash.exe.php)看起來像這樣(左您處理,不被加回顯的答案):
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$num = rand(0, 10000);
$hash = password_hash($num, PASSWORD_BCRYPT, array(
'cost' => 6
));
echo"$hash"; // this 'invisible' HTML output is echo'd back to the page who initiated it
?>
整體放在一起的例子FIDDLE
你將不得不這樣做在Javascript,因爲PHP只執行一次,除非你想刷新網頁 – Deckerz
我覺得很荒謬根據我的回答創建一個[新問題](https://stackoverflow.com/questions/44037952/clicking-ai-tag-to-generate-a-new-hash-wont-work),甚至沒有upvoting任何努力。您可以發表評論,而不是獲得更新的答案。 – Xorifelse