0

Hey there :) I'm trying to make a function so when composing text, user can click button that fetch random value from array and put in textarea no refresh. My current problem is once page load, it get the random value from array in php and js using onclick, but clicking the button dont get new value, always the same until refresh.

I'm not very JS I only use php and some ajax/jquery refresh call. Here my actual function and codes:

 function randommacro(){
$randomword = array(1, 2, 3, 4, 5);
//$randomword = file('macro/randomword.txt');


shuffle($randomword);

$randomword = $randomword[0];
return $randomword;
}


<script language="javascript" type="text/javascript">
function addtext(text) {
document.myform.message.value += text;
}
</script>

<button class="btn btn-primary" onclick="addtext('<?php echo htmlspecialchars(randommacro()); ?>'); return false">Random Macro</button>
3
  • I know this could be usefull but I'm not sure about how to mix both:$("#Readtok").click(function(){ //here t is small letter in ReadTok $("#tokentype").load('../process/read_token_type.php'); }); Commented Jan 30, 2017 at 0:42
  • 1
    could you please provide a addtext function Commented Jan 30, 2017 at 0:45
  • Sorry just putted Commented Jan 30, 2017 at 1:05

2 Answers 2

1

This will allow you to get a random value from your array using javascript.

<html>
<body>
  <script language="javascript" type="text/javascript">
    function addtext() {
      $randomword = ["Name","Think","Person","Apple","Orange","bananna"];
      document.myform.message.value += $randomword[getRandomInt(0,5)];
    }

    function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min)) + min;
    }
  </script>
  <form name="myform">
    <textarea name="message">
      
    </textarea>
    <button type="button" onclick="addtext();">Random Macro</button>
  </form>
</body>
</html>

Here is a PHP and Javascript version:

<html>
<body>
  <?php
    // Create your array using PHP
    $RandomTextArray = array("Name","Think","Person","Apple","Orange","bananna");
  ?>
  <script language="javascript" type="text/javascript">
    function addtext() {
      // Create the Javascript version of your PHP array
      $randomword = <?php echo json_encode($RandomTextArray); ?>;

      // Add a new word to the textarea value
      document.myform.message.value += $randomword[getRandomInt(0,<?php echo sizeof($RandomTextArray) ?>)];
    }

    function getRandomInt(min, max) {
      // Returns a random integer between your min and max values (aka: 0 and size of array)
      return Math.floor(Math.random() * (max - min)) + min;
    }
  </script>
  <form name="myform">
    <textarea name="message"></textarea>
    <button type="button" onclick="addtext();">Random Macro</button>
  </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice, exactly what I was looking for! Can I set $randomword before the script?
0

Try putting it into another variable

$random = $randomword[0]
return $random

2 Comments

Nah it's not the problem, just tried! In fact I want the onclick call the php function, getting random word in my textarea. Each time new word! Current code only echo result of the function then onclick put this unique same value in textarea until reload
Another variable will not solve the problem, you are still just hard coding the value. You need to use a random function to get a random array index. Not hard coded to 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.