Good day, select selects a value, switch determines what action to perform. There is a checkbox in the form along with select . So, if the checkbox is not selected, then nothing happens, the value is sent correctly, but the switch ignore it. If the checkbox is activated, the switch works. Here is the form:

 <form action="" method="post" name="dropdownlist" id="select-interval"> <select class="select rounded shadow" id="interval" name="interval" size="1"> <option disabled selected>Выберите интервал</option> <option value="week">Неделя</option> <option value="month">Месяц</option> <option value="date-selector" data-type="range">Выберите даты...</option> </select> <table id="hidden-table" class="hidden"><tr> <td>Дата начала</td> <td><input type="date" name="start_date"/></td></tr> <tr><td>Дата конца</td> <td><input type="date" name="end_date"/></td></tr></table> <input class="submit color_button rounded" type="submit" value="Выбрать" name="show-select" id="submit" /><Br> <input type="checkbox" name="otchet" value="a1">Создать отчет </form> 

Switch:

 switch($_POST['interval']) { case "week": //Неделя $TemplatePath = 'docs/TemplateWeek.docx'; $table_title = 'Активность за неделю'; $strSQL="SELECT * FROM worktime WHERE pcid='".$_SESSION['currpcid']."' AND year(date)= year(now()) AND week(date, 1) = week(now(), 1) ORDER BY date"; //Выбор записей за неделю break; case "month": //Месяц $TemplatePath = 'docs/TemplateMonth.docx'; $table_title = 'Активность за прошедший месяц'; $strSQL="SELECT * FROM worktime WHERE pcid='".$_SESSION['currpcid']."' AND date > LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) AND date < DATE_ADD(LAST_DAY(CURDATE() - INTERVAL 1 MONTH), INTERVAL 1 DAY) ORDER BY date"; //Выбор записей за неделю break; case "date-selector": //Выбор даты $start_date=$_REQUEST['start_date']; date_create($start_date)->Format('Ym-d'); if (!$_REQUEST['end_date']) { $end_date=date("Ymd"); } else $end_date=$_REQUEST['end_date']; if ($start_date > $end_date) { echo "Начальная дата должна быть меньше даты окончания!"; } date_create($end_date)->Format('Ym-d'); $TemplatePath = 'docs/TemplateInterval.docx'; $table_title = 'Активность с' . $start_date . ' по ' . $end_date . ''; $strSQL="SELECT * FROM worktime WHERE pcid='".$_SESSION['currpcid']."' AND date BETWEEN '$start_date' AND '$end_date' ORDER BY date DESC"; //Выбор записей по интервалу break; } 

Action when the checkbox is pressed:

 if ($_POST['otchet']) { $OtchetDateTime = date('ydm Hi-s'); require_once 'PHPWord.php'; $PHPWord = new PHPWord(); $document = $PHPWord->loadTemplate($TemplatePath); $document->setValue('Value1', '00001'); $document->setValue('Value2', $resultpcname[0]); $document->setValue('Value3', $globaltime); $document->setValue('Value4', $totaltimeout); $document->setValue('Value5', date('Y')); $document->setValue('Value6', date("mdy")); $document->setValue('Value7', $OtchetDateTime); $document->setValue('Value8', $start_date); $document->setValue('Value9', $end_date); $document->save('docs/reports/'. $OtchetDateTime .' '. $_POST['interval']. '-report.docx'); } 
  • Before switch put var_dump($_POST); and look at its contents. - Visman
  • array (size=4) 'interval' => string 'week' (length=4) 'start_date' => string '' (length=0) 'end_date' => string '' (length=0) 'show-select' => string 'Выбрать' (length=14) - g431k

2 answers 2

Depending on the browser, the value of the checkbox will be different (the default value). As a rule, this is 1, on. If the checkbox is not set, then it is not sent to the server. Perhaps the problem is that you have a warning / error. Try to check its existence.

 if(array_key_exists('otchet', $_POST) && $_POST['otchet']){...} 

Ps. It is not very clear how the switch is associated with this condition. It would not be bad if the complete code was presented, reflecting their dependence on each other.

  • The switch and checkbox values ​​are in the same form. I inserted your code to no avail. Switch is executed only with the checkbox pressed - g431k
  • Those. Without a checkbox, a table with records should be displayed (based on a request from a switch), and with a checkbox the same thing + report generation. - g431k

Solved the problem by moving the if ($_POST['otchet']) block if ($_POST['otchet']) from the header to the document body.