简体   繁体   English

在点击PHP / Jquery时更新附加变量

[英]Update appended variable on click PHP / Jquery

I have an array like 我有一个像

     <?php 
        $info = array(
            "cat",
            "dog",
            "bear"
            );
    ?>

And I output one String randomly on an overlay like 我在覆盖层上随机输出一个String

<script>
var text = '<?php echo $info[array_rand ($info)];?>';

$(function() {

var loading = function() {

    var over = '<div id="overlay">' + '<p class="info">' + text + '</p>' + '</div>';         
    $(over).appendTo('body');

    $('#overlay').click(function() {
        $(this).remove();
    });


    $(document).keyup(function(e) {
        if (e.which === 27) {
            $('#overlay').remove();
        }
    });
};

$('.wrapper').click(loading);

});
</script>

CSS: CSS:

#overlay {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
background: #000;
opacity: 0.90;
height: 200%;
}

.info{
position: absolute;
width: 100%;
top: 25%;
height: 200px;
font-size: 50px;
color: white;
text-align: center;
}

My question is: How can I update the var text and get a new random string every time the overlay is opened by clicking on the body? 我的问题是:每次通过单击主体打开覆盖层时,如何更新var文本并获得新的随机字符串? Till now the string in var text is only updated when I reload the whole page. 到现在为止,仅当我重新加载整个页面时,才会更新var文本中的字符串。

Thanx :) 谢谢:)

Print your PHP array in the page using JSON: 使用JSON在页面中打印PHP数组:

<!-- language: lang-php -->

<?php
$info = array(
            "cat",
            "dog",
            "bear"
            );
?>

<!-- language: lang-js -->

<script type="text/javascript">
// (it's better to retrieve it in ajax)
var arr = <?php echo json_encode($info); ?>;

//  2. Roll some dice in Javascript
// Math.random returns a random value between 0 and 1
var text = "";
// wrapped in a function so you can pick new random values
function pickRandom() {
    // Math.random returns a decimal number, and you need to access arrays using their indexes (0, 1, 2) not decimal numbers. Number.parseInt does the rounding.
    text = arr[ Number.parseInt( Math.random()*arr.length ) ];
    alert("new random value is: " + text);
}
pickRandom();
</script>

Then the rest of your code should work. 然后,其余的代码应该可以工作。 Hope this helps. 希望这可以帮助。

Doc: 文档:

You cannot because PHP is executed on the server side. 您不能因为PHP在服务器端执行。

When you load the page, it sends to your browser the final HTML, with the result of <?php echo $info[array_rand ($info)];?> . 加载页面时,页面将最终HTML发送给浏览器,结果为<?php echo $info[array_rand ($info)];?>

If security is not an issue (games, banking...) you can use randomness using JavaScript. 如果安全性不是问题(游戏,银行业务...),则可以使用JavaScript使用随机性。

Otherwise you can reload the page manually like so: 否则,您可以像这样手动重新加载页面:

$('#overlay').click(function() {
    // reload the page silently. see also http://stackoverflow.com/questions/2624111/preferred-method-to-reload-page-with-javascript
    window.location.href = window.location.href;
    //$(this).remove();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM