I am working on a random word generator, I have made a word appear every time I press a button:
<?php
$frases = array(
1 => "Una lavadora",
2 => "Un movil",
3 => "Una muñeca",
);
$numero = rand (1,3);
echo "$frases[$numero]";
?>
<input type="button" value="iniciar" onclick="document.location.reload();">
1-How can I make it so that the third time I press the button, the word YOU HAVE PRESSED THE BUTTON THREE TIMES! and stop generating words?
2-Is there any way to do it without reloading the entire page?
Adding only one counter and requesting the word by AJAX.
PHP
HTML
JS
The above will work only as long as the document is not reloaded, in case it is reloaded, it will have its three words again. In case you want it to be while the user is logged in, you can use a cookie instead of the counter or better yet
sessionStorage
.Another way to do it:
PHP
HTML
javascript
We convert a PHP object to JavaScript using
var arrayJS=<?php echo json_encode($arrayPHP);?>;
.I have had a problem when converting the object and it is that the index of the array in php must start at 0.