1

I want to display several months at once. Currently showing only one month. When I try to select multiple months, it only shows one. What could be wrong with my code? Thank you very much for the reply.

Here is my code:

for($i=1;$i<=30;$i++){
    $place_of_counselling=get_field('noustamise_koht'.$i.'',$post->ID);
    $date=date_create(get_field('noustamise_kuupaev'.$i.'',$post->ID));
    if(date('m')!= date_format($date,"m") && !isset($_POST['month'])){
        continue;
    }elseif($_POST['month'] != date_format($date,"m")){   
        continue;
    }

and the form code:

<form action="" method="post">
                    <input type="checkbox" id="01" name="month" value="01"> Jaanuar<br>
                    <input type="checkbox" id="02" name="month" value="02"> Veebruar<br>
                    <input type="checkbox" id="03" name="month" value="03"> Märts<br>
                    <input type="checkbox" id="04" name="month" value="04"> Aprill<br>
                    <input type="checkbox" id="05" name="month" value="05"> Mai<br>
                    <input type="checkbox" id="06" name="month" value="06"> Juuni<br>
                    <input type="checkbox" id="07" name="month" value="07"> Juuli<br>
                    <input type="checkbox" id="08" name="month" value="08"> August<br>
                    <input type="checkbox" id="09" name="month" value="09"> September<br>
                    <input type="checkbox" id="10" name="month" value="10"> Oktoober<br>
                    <input type="checkbox" id="11" name="month" value="11"> November<br>
                    <input type="checkbox" id="12" name="month" value="12"> Detsember<br>
                
                    <input type="submit" value="Otsi" name="submitbutton" >
            </form>
Margus
  • 11
  • 3
  • 2
    You need to give them different names, or use an array `name="month[]"` – Barmar Jul 13 '22 at 15:23
  • Does this answer your question? [How to get a form input array into a PHP array](https://stackoverflow.com/questions/3314567/how-to-get-a-form-input-array-into-a-php-array) – WOUNDEDStevenJones Jul 13 '22 at 18:51

1 Answers1

0

When a POST request is sent to the server, only the name attribute shows up in the $_POST array variable. The keys comes from the name attribute of the HTML form input. The id, and value attributes don't get sent to the server. Since all of your name attributes are the same, month, it only sends one of them as $_POST["month"]. If you want all of them, each name attribute has to be unique. Like this:

<input type="checkbox" id="01" name="month" value="01"> Jaanuar<br>
                    <input type="checkbox" id="02" name="month02" value="02"> Veebruar<br>
                    <input type="checkbox" id="03" name="month03" value="03"> Märts<br>
                    <input type="checkbox" id="04" name="month04" value="04"> Aprill<br>
                    <input type="checkbox" id="05" name="month05" value="05"> Mai<br>
                    <input type="checkbox" id="06" name="month06" value="06"> Juuni<br>
                    <input type="checkbox" id="07" name="month07" value="07"> Juuli<br>
                    <input type="checkbox" id="08" name="month08" value="08"> August<br>
                    <input type="checkbox" id="09" name="month09" value="09"> September<br>
                    <input type="checkbox" id="10" name="month10" value="10"> Oktoober<br>
                    <input type="checkbox" id="11" name="month11" value="11"> November<br>
                    <input type="checkbox" id="12" name="month12" value="12"> Detsember<br>

Then you'll have to adjust the php code to look for these different keys.

Tyler Dill
  • 351
  • 2
  • 6