There is a html record form as well as its handler that records the entered data in the mysql and displays it on the screen.

The data is entered into the database and the information from the form is displayed on the html page, but there is a problem with the output of information from the select element (drop-down list), it is necessary that the selected word be displayed, and not its code.

Record Form

 <form class="contact_form" action="message.php" method="post" name="contact_form"> <br> <ul> <li> <h2>Введите данные для записи на прием</h2> <span class="required_notification">* Поля, обязательные для заполнения</span> </li> <li> <label for="name">Имя:</label> <input type="text" name="name" required placeholder="Ваше имя"> </li> <li> <label for="surname">Фамилия:</label> <input type="text" name="surname" required placeholder="Ваша фамилия"> </li> <li> <label for="middlename">Отчество:</label> <input type="text" name="middlename" required placeholder="Ваше отчество"> </li> <li> <label for="policynumber">Номер полиса:</label> <input type="text" name="policynumber" required placeholder="Ваш номер полиса"> </li> <li> <label for="doc">Выберите врача:</label> <select name="doctor"> <option value="1" selected>Терапевт</option> <option value="2">Стоматолог</option> </select> </li> <li> <label for="dateofbirthr">Дата рождения:</label> <input type="date" name="dateofbirth" required placeholder="Дата рождения"> </li> <li> <label for="dateofadmission">Выберите дату приема:</label> <input type="date" name="dateofadmission" required placeholder="Дата приема"> </li> <li> <button class="submit" type="submit">Отправить</button> </li> </ul> </form> 

Form handler (did not copy it completely, missed the moment with connecting to the database)

 try { switch ($doctor) { case 1: $table = "therapist"; break; case 2: $table = "dentist"; break; } $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->beginTransaction(); $sth = $dbh->prepare("INSERT INTO `$table` (name, surname, middlename, dateofbirth, policynumber, dateofadmission) VALUES (:name, :surname, :middlename, :dateofbirth, :policynumber, :dateofadmission)"); // Для вставки параметров в запрос PDO есть специальные методы $sth->bindParam(':name', $name, PDO::PARAM_STR); $sth->bindParam(':surname', $surname, PDO::PARAM_STR); $sth->bindParam(':middlename', $middlename, PDO::PARAM_STR); $sth->bindParam(':dateofbirth', $dateofbirth, PDO::PARAM_STR); $sth->bindParam(':policynumber', $policynumber, PDO::PARAM_INT); $sth->bindParam(':dateofadmission', $dateofadmission, PDO::PARAM_STR); $sth->execute(); } catch (Exception $e) { $dbh->rollBack(); } print (" Ваше имя: " . $_POST['name'] . ".<br>\n"); print (" Ваша фамилия: " . $_POST['surname'] . ".<br>\n"); print (" Ваше отчество: " . $_POST['middlename'] . ".<br>\n"); print (" Дата рождения: " . $_POST['dateofbirth'] . ".<br>\n"); print (" Номер полиса: " . $_POST['policynumber'] . ".<br>\n"); print (" Дата приема: " . $_POST['dateofadmission'] . ".<br>\n"); print (" Лечащий врач: " . $_POST['doctor'] . ".<br>\n"); { $dbh->commit(); } } ?> 

Here is what is displayed on the screen (note the last line) enter image description here

  • Change the value of the select, or make a switch in the - Cyril Zhelnov
  • Horrible code, do not use print - use echo - this construction supports the operator and so on (and so on), that is, echo $a, $b, $c; - listing of arguments separated by comma, for the concatenation point $a.$b.$c and works faster than print . And I recommend using mysqli with the mysqlnd driver. - And

2 answers 2

 $posts = [ 1 => 'Терапевт', 'Стоматолог' ]; print (" Лечащий врач: " . $posts[$_POST['doctor']] . ".<br>\n"); 
  • I used this option as I have some other version of php $ posts = array (1 => 'Therapist', 'Dentist'); - SergSKH
  • @SergSKH you pass option'a number, try a better option that suggested Web Desg, it works much more logical and will be more convenient to edit. - Telion

You have everything correctly displayed.

 <select name="doctor"> <option value="1" selected>Терапевт</option> <option value="2">Стоматолог</option> </select> В данном случае значение $_POST['doctor'] будет 1 или 2 для того чтобы вывести профессию нужно написать вроде этого: <select name="doctor"> <option value="Терапевт" selected>Терапевт</option> <option value="Стоматолог">Стоматолог</option> </select>