1

I have an array like

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

And I output one String randomly on an overlay like

<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:

#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? Till now the string in var text is only updated when I reload the whole page.

Thanx :)

2 Answers2

1

Print your PHP array in the page using JSON:

<!-- 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:

pyb
  • 4,813
  • 2
  • 27
  • 45
  • Hm somehow it's returning an undefined random value :/ What's wrong with it? – Pierre Gueba Jan 27 '15 at 18:20
  • Can you share the page source? (File > View source or Ctrl+U). I wrote it on the top of my head. – pyb Jan 27 '15 at 18:58
  • also try ```var arr = JSON.parse(''); ``` – pyb Jan 27 '15 at 19:02
  • Actually here's the link to the page :D The text comes from the twitter-API, The BG-Images from the Flickr-API..Just klick somwhere in the body to open the overlay..I'm so close but those strings wont be refreshed when opening the overlay :( http://www.merz-akademie.de/users/peer.guba/alltag/alltag.php – Pierre Gueba Jan 27 '15 at 19:04
  • @PierreGueba I don't see where you put my code, I just see the original one. – pyb Jan 27 '15 at 19:06
  • wait a second please and refresh ;) – Pierre Gueba Jan 27 '15 at 19:11
  • try ```var text = arr[ Number.parseInt( Math.random()*arr.length ) ]```. Math.random returns a decimal number, and you need to access arrays using their indexes (0, 1, 2) not decimal numbers. – pyb Jan 27 '15 at 19:43
  • I pasted it in and the shuffle seems to work (for the alert)..unfortunately nothing is shown in the overlay itself anymore :/ – Pierre Gueba Jan 27 '15 at 19:51
  • You put ```var text = arr[ Number.parseInt( Math.random()*arr.length ) ]``` you should put ```text = arr[ Number.parseInt( Math.random()*arr.length ) ];``` without the var. – pyb Jan 27 '15 at 20:12
  • Suprise suprise the value is appearing! :) But did this really fix the issue? I'm getting the very same value when I close and reopen the overlay..Can't I set a timer or something in order not to be forced to refresh the page to get a new value? – Pierre Gueba Jan 27 '15 at 20:23
  • maybe something like http://stackoverflow.com/questions/6962658/randomize-setinterval-how-to-rewrite-same-random-after-random-interval ? – Pierre Gueba Jan 27 '15 at 20:31
  • you need to call pickRandom() when closing or opening the overlay – pyb Jan 27 '15 at 20:37
  • or regenerate the whole overlay. Wrap the(randomness + html code for the overlay in a function, and call that function at will. – pyb Jan 27 '15 at 20:38
  • Man, how would I ever do that? :D ...Pleeeease!! :) – Pierre Gueba Jan 27 '15 at 20:40
  • Move my code right above ```var over = '
    ' + '

    ' + text + '

    ' + '
    ';``` and add alert or something to help you understand what this does.
    – pyb Jan 27 '15 at 20:48
0

You cannot because PHP is executed on the server side.

When you load the page, it sends to your browser the final HTML, with the result of <?php echo $info[array_rand ($info)];?>.

If security is not an issue (games, banking...) you can use randomness using 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();
});
pyb
  • 4,813
  • 2
  • 27
  • 45
  • reload the page just to change overlay text?? – charlietfl Jan 26 '15 at 23:57
  • Well, someone suggested Ajax but you did not answer to this suggestion. That's better, I agree. – pyb Jan 27 '15 at 00:01
  • Thank you for the quick answers! I was asleep because it was late in europe when I posted my question.. The manual thing with window.location.href = window.location.href; doesn't fix the issue because I don't want to reload the whole page..but can you tell me more about the JavaScript-Randomness? Security is not an issue as it's for kind of an art project...The main goal is to only refresh the content of the overlay every time it gets opened. So it doesn't have to be PHP either..A pure JS-Solution would be nice too! Thanx dudes! :) – Pierre Gueba Jan 27 '15 at 10:20