-1

I have a form with checkboxes.. and I need to send one at random as a variable.

Check more checkboxes and write one at random:

<form action="index.php" method="POST">
 <input type="text" name="name" placeholder="Jméno vyvolávače" maxlength="32"><p>
 <table>
  <tr>
   <input type="checkbox" onClick="toggle(this)" /> Vyber vše<br/>
   <td>Ahri<input type="checkbox" name="champ" value="Ahri"></td>
   <td>Aatrox<input type="checkbox" name="champ" value="Aatrox"></td>
   <td>xx<input type="checkbox" name="champ" value="xx"></td>
   <td>xx<input type="checkbox" name="champ" value="xx"></td>
  </tr>
 </table>
 <input type="submit">
</form>
<?php
 echo "Vyvolávači ".$name," pickni si ".$champ," na ".$line;
?>

So now I check Ahri and Aatrox.. and page submits one at random. How can I do this?

LSerni
  • 55,617
  • 10
  • 65
  • 107
Lexikonn
  • 15
  • 4
  • 3
    Send all checkboxes to PHP, using "champ[]" (note the brackets) as a name. You will receive an array. use shuffle() on it to rearrange it at random. Pop one element from the array. You're done. – LSerni Oct 10 '17 at 20:06
  • I dont understand how u mean this.. can U write code please? – Lexikonn Oct 10 '17 at 20:16
  • @Lexikonn If my answer helped you, please be sure to mark it as correct. If it did not help you, please let me know what is not working. Thank you! – GrumpyCrouton Oct 11 '17 at 12:59

1 Answers1

4

Your current code will not work, because all of the checkboxes have the same name. This means that the last checkbox on the page that is checked will always be the only value sent by the form, as it will be overwritten.

There is a solution! By adding square brackets ([]) to the variable name, you are telling the form to send the values as an array, so that all checked options are sent with the form.

If you look at the code below, I added the square brackets to the "name" attribute. You can find more information on the markup for form elements to become arrays on this StackOverflow post

<td>
    Ahri
    <input type="checkbox" name="champ[]" value="Ahri">
</td>
<td>
    Aatrox
    <input type="checkbox" name="champ[]" value="Aatrox">
</td>

Now, you can randomize the array using shuffle(), this makes it so you can take the first element off the array using array_pop(), which, at this point will be a random value based on the checkboxes that have been selected.

When a checkbox is not selected, the value for it is not passed to the form, so this allows you to randomly select between only selected checkboxes.

if(isset($_POST['champ'])) {

    /*I assign the array to a regular PHP variable as it's considered 
    bad practice to alter GLOBAL variables. If no choices are selected,
    you will always get the value of "No Options Selected" because of the ternary.*/
    $champ = !empty($_POST['champ'])? $_POST['champ'] : ["No Options Selected"];

    /*shuffle the array, randomizing the location of each value*/
    shuffle($champ);

    /*array pop takes the first element off the array, and returns the value.
    So we set $randomValue to the first element in the array basically.*/
    $randomValue = array_pop($champ);

    /*You could also overwrite the array if you want to use 
    the $champ variable.*/
    //$champ = array_pop($champ); 

    /*alternatively, you could do `$randomValue = $champ[0]` for the same result, 
    except the array will not lose the first element.*/
    //$randomValue = $champ[0];

    /*You could also overwrite the array if you want to use 
    the $champ variable.*/
    //$champ = $champ[0];

}

$randomValue will now contain a random value based on the selected checkboxes.

echo "Vyvolávači ".$name," pickni si ".$randomValue," na ".$line;

-Update to this section: I changed the variable assignment of $champ to check if the $_POST['champ'] checkboxes are empty() using a ternary. This makes it so that if no checkboxes have been checked in the form page, it substitutes another array which only contains a single element which says "No Options Selected", this will always be the return if no options are selected.


You're using PHP, why aren't you utilizing it?

As a side note, you can get rid of a lot of HTML by using a loop with possible values. This can prevent a lot of typos and syntax issues, and allow you to more easily control the options the user has to choose from as you can easily build arrays based on your database or manually build an array to make sure all the code is exactly the same for every checkbox.

$possibleValues = [
    "Ahri",
    "Aatrox",
    "xx1",
    "xx2",
];

foreach($possibleValues as $value) {
    echo "<td>{$value}<input type='checkbox' name='champ[]' value='{$value}'/></td>";
}

Side Note #2

Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help you debug your code. Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end.

As you can see in the code I supplied above (In the HTML and PHP), every line has a specific amount of spacing that makes the code more readable. It's also beneficial to break elements with multiple sub elements into multiple lines (most of the time), which makes it way more readable and easier to work with.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71