单击<i>标记以生成新哈希(赢得了工作)

时间:2017-05-18 03:07:16

标签: php html ajax

由于某些原因,这只会使<span>中的文字变为空白。我点击<i>标记时尝试这样做,它会在<span>标记中生成新的哈希值。

HTML:

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

AJAX:

$('#refresh-hash').click(function(){
  $.post( "../api/register_hash.php", function(data) {
    $(".reg-code > span").html(data);
  });
}); 

PHP:

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

1 个答案:

答案 0 :(得分:0)

你可以采取一些措施使事情发挥作用:

# Look for an action word
if($_POST['action'] == 'get_hash') {
    $num = rand(0, 10000);
    $hash = password_hash($num, PASSWORD_BCRYPT, array(
        'cost' => 6
    ));
    # Die (echo here)
    die($hash);
}

我不知道<i id="refresh-hash">块中应该是什么,但没有任何内容可供点击,<i>为空。我添加了“REFRESH”这个词,所以有点可点击。

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

<script>
$(document).ready(function() {
    // Try using "on()" here
    $('#refresh-hash').on('click',function(){
        // Send data (action word)
        // Also make sure the "../api/register_hash.php" is actually the
        // correct path
        $.post("../api/register_hash.php", {"action":'get_hash'},function(data) {
            console.log(data);
            $(".reg-code > span").html(data);
        });
    });
});
</script>

如果你想知道,我确实检查了这个,如果正确实施它确实有效。可能你的主要问题是你没有回应php端的回复(die($hash);)。