2017-05-17 29 views
0
$num = rand(0, 10000); 
$hash = password_hash($num, PASSWORD_BCRYPT, array(
    'cost' => 6 
)); 

以上是哈希單擊文本標籤生成新的散列

<p class="reg-code">Hash: <span><?php echo $hash; ?></span><i class="fa fa-refresh" aria-hidden="true" id="refresh-code"></i></p> 

我想它,所以當有人點擊,它生成一個新的哈希到跨度中的標籤,我會怎麼來的跨越這個?

我不知道我會如何做到這一點,就像在PHP或什麼功能。

+0

你將不得不這樣做在Javascript,因爲PHP只執行一次,除非你想刷新網頁 – Deckerz

+0

我覺得很荒謬根據我的回答創建一個[新問題](https://stackoverflow.com/questions/44037952/clicking-ai-tag-to-generate-a-new-hash-wont-work),甚至沒有upvoting任何努力。您可以發表評論,而不是獲得更新的答案。 – Xorifelse

回答

0

這就是所謂的Ajax:

<p class="reg-code"> 
    Hash: 
    <span id="hash"><?php echo $hash; ?></span> 
    <i id="click" class="fa fa-refresh" aria-hidden="true" id="refresh-code"></i> 
</p> 

$('#click').click(function(){ 
    $.post("./path/to/hashscript.php", function(data) { 
    $("#hash").html(data); 
    }); 
}); 

您可以使用JavaScript事件,使服務器請求和更新與響應的頁面元素。你的PHP腳本應該回顯$hash

echo password_hash($num, PASSWORD_BCRYPT, ['cost' => 6]); 
+0

你好, 現在當我點擊標記時,散列從中消失,但不會生成新的標記? – Oliver

+0

那麼很可能你沒有調用正確的腳本。試着用'。/ path/to/hashscript.php'來擺弄。此外,你應該使用元素ID而不是類。 – Xorifelse

+0

@Oliver更新了代碼以使用我想要的,因爲它應該是。 – Xorifelse

0

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

0

應該得到。因爲它不會更改服務器狀態。只需返回新的價值。

$('.fa-refresh').click(function(){ 
    $.get("./path/to/hashscript.php", function(data) { 
    $(".reg-code > span").html("Hash:" + data); 
    }); 
}); 

和hashscript.php文件應包含

echo md5(uniqid(rand(), TRUE)); 
+0

不錯的副本,你的改變是不正確的。 'hashscript.php'應該回顯'$ hash'而不是隨機的散列值。 – Xorifelse

+0

是的,它是一個副本。但是,如果你睜開眼睛看,你會發現它們之間有什麼區別。而且沒有什麼區別,你可以打印任何你想要的東西只是一個想法。 –