Php 单击文本标记以生成新哈希

Php 单击文本标记以生成新哈希,php,jquery,html,ajax,Php,Jquery,Html,Ajax,上面是散列 $num = rand(0, 10000); $hash = password_hash($num, PASSWORD_BCRYPT, array( 'cost' => 6 )); 散列: 我希望这样,当有人点击标签,它会生成一个新的散列到跨度,我怎么会遇到这个 我不知道该怎么做,比如在PHP中作为函数还是什么。它被称为Ajax: <p class="reg-code">Hash: <span><?php echo $hash; ?&g

上面是散列

$num = rand(0, 10000);
$hash = password_hash($num, PASSWORD_BCRYPT, array(
    'cost' => 6
));

散列:

我希望这样,当有人点击标签,它会生成一个新的散列到跨度,我怎么会遇到这个

我不知道该怎么做,比如在PHP中作为函数还是什么。

它被称为Ajax:

<p class="reg-code">Hash: <span><?php echo $hash; ?></span><i class="fa fa-refresh" aria-hidden="true" id="refresh-code"></i></p>
Ajax允许您将请求发送到另一个页面而无需刷新。 检查jQuery的这一部分

在您的情况下,设置如下(首先,在页面的标题中包括jQuery):

PHP(hash.exe.PHP)将如下所示(让您按原样处理,添加了echo的答案):


它应该被获取。因为它不会更改服务器状态。只需返回新值

<?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
?>
而hashscript.php文件应该包含

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

你必须用Javascript来实现这一点,因为php只执行一次,除非你想刷新网页。我觉得基于我的答案创建一个基于我的答案的网页,而不需要任何人的努力,这是荒谬的。您可以通过添加注释来获得更新的答案。您好,现在当我单击标记时,哈希值会从中消失,但不会生成新的哈希值?那么您很可能没有调用正确的脚本。试着摆弄
/path/to/hashscript.php
。另外,您可能应该使用元素id而不是类。@Oliver更新了代码以使用I'd,这是应该的。复制得很好,您的更改不正确
hashscript.php
应该回显
$hash
,而不是随机散列值。是的,它是一个副本。但是如果你睁开眼睛看,你会发现有什么不同,而这并没有什么不同,你可以打印任何你想要的东西,这只是一个想法。
<p class="reg-code">Hash: <span></span>
<i class="fa fa-refresh" aria-hidden="true" id="refresh-code"></i></p>
$(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
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
?>
$('.fa-refresh').click(function(){
   $.get( "./path/to/hashscript.php", function( data ) {
    $( ".reg-code > span" ).html( "Hash:" + data );
   });
});
echo md5(uniqid(rand(), TRUE));