Why other options aren't in ?
<select name="to">
<?php
foreach($students as $student) {
?>
<option value="<?php echo($student['name'] . ' ' . $student['surname']); ?>"><?php echo($student["name"] . " " . $student["surname"]); ?></option>
<input type="hidden" name="name" value="<?php echo($student['name']);?>">
<input type="hidden" name="surname" value="<?php echo($student['surname']);?>">
<?php
}
foreach($teachers as $teacher) {
?>
<option value="<?php echo($teacher['name'] . ' ' . $teacher['surname']); ?>"><?php echo($teacher["name"] . " " . $teacher["surname"]); ?></option>
<input type="hidden" name="name" value="<?php echo($student['name']);?>">
<input type="hidden" name="surname" value="<?php echo($student['surname']);?>">
<?php
}
?>
</select>
That is how it looks:
<select name="to">
<option value="BOBO BO">BOBO BO</option>
<input type="hidden" name="name" value="BOBO">
<input type="hidden" name="surname" value="BO">
<option value="XD MAN">XD MAN</option>
<input type="hidden" name="name" value="XD">
<input type="hidden" name="surname" value="MAN">
<option value="IVA OVOO">IVA OVOO</option>
<input type="hidden" name="name" value="XD">
<input type="hidden" name="surname" value="MAN">
<option value="LOKO LOOKO">LOKO LOOKO</option>
<input type="hidden" name="name" value="XD">
<input type="hidden" name="surname" value="MAN">
</select>
And why is XD MAN under two another person?
I have to get name and surname separately, that is why I save them to hidden input.
There are multiple issues with your PHP code:
1.) In the second foreach
you still use $student
in the second and third echo. Thats why you get the same name again and again.
BTW you don't have to use parenthesis with echo.
2.) Like other people already commented on your question: The output is invalid HTML anyways, which is probably the reason why your browser is not displaying all <option>
s correctly. You just cannot have <input>
s inside of a <select>
.
Why don't you just use some special character (lets say +
) to separate the name and the surname in the <option>
s value attribute and then use explode('+',$_POST['to'])
(or $_GET
respectively) when you get the results back on the server? This way you could get the name and surname separately.