Randomize according to a radio button

Asked

Viewed 151 times

0

I want to make use of "Radio Buttons", HTML but I don’t know how to use them. How to make them I know, but I need something like this:

If I use option one, generate a random word being Language, Language.

If I use option two, generate a random word being Language, Dialect.

I can generate a random word with PHP, I need to know how to "distribute" both.

  • Hello, welcome to the OS in Portuguese. Try to explain your need better, including telling us what you already have. Consider doing the tour and also how to create a Minimum, Complete and Verifiable example.

  • Have you tried using Jquery to become dynamic(when choosing one does the action A when choosing the other does the action B)

  • @Thallesdaniel How do I do this? I am zeroed in Javascript and jquery

  • Since you are from "0" see an example I made for you to have an idea of what you can do with jquery

  • How’s your code for generating random words

1 answer

2

Look at it this way:

<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
  $('#radioA').click(function() {
    var data = 'id=aleaa';
    $.get('aleatoria.php', data, function(response){
      $('#dv').html(response);
    });
  });
  $('#radioB').click(function() {
    var data = 'id=aleab';
    $.get('aleatoria.php', data, function(response){
      $('#dv').html(response);
    });
  });
});
</script>

<div id="dv"></div><br/>
<input type="radio" name="group1" value="aleatoria" id="radioA"> aleatório A<br>

<input type="radio" name="group1" value="aleatoria" id="radioB"> aleatório B<br>
<!--<button id="buton">Click</button>-->

</body>

and create a randomized.php file like this:

<?php
    if($_GET["id"] == "aleaa")
    {
    $palavras = array('Língua','Linguagem'); 
    $aleatorio = rand(0,1); 
    echo $palavras[$aleatorio];
    } 
    elseif($_GET["id"] == "aleab")
    {
    $palavras = array('Idioma','Dialeto'); 
    $aleatorio = rand(0,1); 
    echo $palavras[$aleatorio];
    }
 ?>

jquery does a GET in the.php random file if the parameter(?id=Alea) is correct it takes the random word it generated. If the answers are satisfactory I ask you to point out :)

  • That’s what I know how to do friend. I want radio buttons, one randomizes one thing and another

  • if you know how to do it then just switch the button for the radio see.

  • I have updated the code the way you want a radioA does a language and language randomness and radioB does Language and Dialect. The code is ready.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.