javascript - Why is my second <select> tag is not showing up?
I am trying to display <select id="second_choice">
tag after I choose one of the options in <select id="first_choice">
. Both are selected from my database.
technician.php
<select name="cntNum" id="first_choice" class="form-control" onchange="fetch_select(this.value);" required>
<?php
include('config.php');
$query = "SELECT cntNum FROM contract";
$result = mysqli_query($link,$query);
while($row = mysqli_fetch_array($result)) {
echo "<option>".$row{'cntNum'}."</option>";
}
</select>
<select name="cntNum" id="second_choice" class="form-control" required></select>
technician_fetch.php
require_once( 'config.php' );
$choice = mysqli_real_escape_string($_GET['choice']);
$query = "SELECT * FROM equipment WHERE cntNum = $choice";
$result = mysqli_query($link,$query);
while($row = mysqli_fetch_array($result)){
echo "<option>" . $row{'cntNum'} . "</option>";
}
jQuery
<script>
$("#first_choice").change(function () {
$("#second_choice").load("technician_fetch.php?choice=" + $( "#first_choice").val());
});
</script>
Answer
Solution:
You need to close your PHP before writing anymore HTML.
<select name="cntNum" id="first_choice" class="form-control" onchange="fetch_select(this.value);" required>
<?php
include('config.php');
$query = "SELECT cntNum FROM contract";
$result = mysqli_query($link,$query);
while($row = mysqli_fetch_array($result)) {
echo "<option>".$row{'cntNum'}."</option>";
}
?>
<!-- PHP must be closed if you then continue with HTML -->
</select>
<select name="cntNum" id="second_choice" class="form-control" required></select>
If you are just writing PHP in a file then you don't need to close it but if you have a different language after it (HTML) you must close it.
Source