There is such code:

<form action="" method="GET"> <p>html<input type="checkbox" name="lg" value="1"></p> <p>css<input type="checkbox" name="lg" value="2"></p> <p>php<input type="checkbox" name="lg" value="3"></p> <p>javascript<input type="checkbox" name="lg" value="4"></p> <input type="submit"> </form> <?php $str = 'Вы знаете: '; if(isset($_REQUEST['lg']) and $_REQUEST['lg'] == 1) { $str .= 'html.'; } if(isset($_REQUEST['lg']) and $_REQUEST['lg'] == 2) { $str .= 'css.'; } if(isset($_REQUEST['lg']) and $_REQUEST['lg'] == 3) { $str .= 'php.'; } if(isset($_REQUEST['lg']) and $_REQUEST['lg'] == 4) { $str .= 'javascript.'; } echo $str; ?> 

The problem is that if the user tick a few checkboxes, then only one of the selected ones will be displayed. And it is necessary that everything near which put a tick.

What needs to be added to the code to implement it?

    2 answers 2

    Based on the previous answer, plus optimization:

     <form action="" method="GET"> <p>html<input type="checkbox" name="lg[]" value="html"></p> <p>css<input type="checkbox" name="lg[]" value="css"></p> <p>php<input type="checkbox" name="lg[]" value="php"></p> <p>javascript<input type="checkbox" name="lg[]" value="javascript"></p> <input type="submit"> </form> <?php $lg = is_array($_REQUEST['lg']) ? $_REQUEST['lg'] : array(); echo 'Вы знаете: ' . join('.', $lg); ?> 
    • error join gives. You can upload my code, just reworked correctly, otherwise either I change the wrong way or something is not quite right ( - Beginner
    • Slightly corrected. - YuS
    • The main thing is that you understand the principle, and not that everything is written for you, is it? - YuS
    • Thank you, Yuri. Excellent - short and almost understandable!) Of course, I want to understand the principle of work and I have a question. Please comment on this expression $ lg = is_array ($ _ REQUEST ['lg'])? $ _REQUEST ['lg']: array (); - Beginner
    • As I understand it - this expression says that if an array element is requested, then output it, and if not, then array (); And why did you decide array (); - this is not my moumu yet - Beginner

    It is necessary for the input to change the name to name="lg[]" , then in $_REQUEST['lg'] will be an array of selected values

    UPD Updated your code, but you have to think about solution optimization and refactoring

     <form action="" method="GET"> <p>html<input type="checkbox" name="lg[]" value="1"></p> <p>css<input type="checkbox" name="lg[]" value="2"></p> <p>php<input type="checkbox" name="lg[]" value="3"></p> <p>javascript<input type="checkbox" name="lg[]" value="4"></p> <input type="submit"> </form> <?php $str = 'Вы знаете: '; if(isset($_REQUEST['lg'])) { if(in_array(1, $_REQUEST['lg'])) { $str .= 'html.'; } if(in_array(2, $_REQUEST['lg'])) { $str .= 'css.'; } if(in_array(3, $_REQUEST['lg'])) { $str .= 'php.'; } if(in_array(4, $_REQUEST['lg'])) { $str .= 'javascript.'; } } echo $str; ?> 
    • I replaced lg with lg [] in the form and in general no selected checkbox is displayed. Then in requests I also replaced lg with lg [] and nothing. - Beginner
    • Now you need to check as an array in_array (1, $_REQUEST['lg'], true) and not as a string - OotzlyGootzly
    • And so $_REQUEST['lg[]'] not necessary to write, just like that $_REQUEST['lg'] - OotzlyGootzly
    • Eh, as far as I understood, I tried to replace it in the code with what you wrote, but the script does not work for me (( - Beginner
    • one
      yes, now ok, thanks !! - Beginner