Why can't I get the value of the element? sorry I'm new at coding and trying to learn
<script>
function getdll() {
document.getElementById('lblmess').innerHTML =
(formid.listid[formid.listid.selectedIndex].value)
$element = document.getElementById("lblmess");
console.log($element.innerHTML)
}
</script>
<form name="formid">
<select class="custom-select" name="listid" onchange="getdll()">
<option value="0"></option>
<?php
$hall_qry = $conn->query("SELECT * FROM `office_hall` WHERE o_id !=0 order by `office_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['o_id'] ?>"><?php echo $row['office_name'] ?></option>
<?php
endwhile;
?>
</select>
<br><br>
<select class="custom-select">
<?php
$hall_qry = $conn->query("SELECT * FROM `assembly_hall` WHERE o_id = '$element' order by `room_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['room_name'] ?></option>
<?php
endwhile;
?>
</select>
<label id="lblmess"></label>
</form>
this is where I use this code sorry if my coding id is like that .............................................................
My Answer:
It looks like you are trying to get the selected value from the first dropdown and use it to load data into the second dropdown. However, there are a few issues in your code.
1. In your PHP code, you are trying to use the variable `$element` to fetch data from the database. However, `$element` is a JavaScript variable and cannot be directly used in PHP code. You need to send this value to the server using AJAX or form submission.
2. You are trying to access the value of `$element` in the PHP code before it is set. The value of `$element` is set in the JavaScript function `getdll()`, so you cannot directly use it in the PHP code.
To fix this issue, you can modify your code as follows:
1. Modify your JavaScript function to send the selected value to the server using AJAX:
javascript
function getdll() {
var selectedValue = formid.listid.options[formid.listid.selectedIndex].value;
document.getElementById('lblmess').innerHTML = selectedValue;
// Send the selected value to the server using AJAX
var xhr = new XMLHttpRequest();
xhr.open('GET', 'get_data.php?selectedValue=' + selectedValue, true);
xhr.onload = function() {
if (xhr.status === 200) {
// Update the second dropdown with the data received from the server
document.getElementById('secondDropdown').innerHTML = xhr.responseText;
}
};
xhr.send();
}
2. Create a PHP file (e.g., `get_data.php`) to handle the AJAX request and fetch data from the database based on the selected value:
php
// get_data.php
// Get the selected value from the AJAX request
$selectedValue = $_GET['selectedValue'];
// Fetch data from the database based on the selected value
$hall_qry = $conn->query("SELECT * FROM `assembly_hall` WHERE o_id = '$selectedValue' order by `room_name` asc");
while ($row = $hall_qry->fetch_assoc()) {
echo '';
}
?>
3. Update your HTML code to include a second dropdown with the id `secondDropdown`:
html
By making these changes, you should be able to get the selected value from the first dropdown, send it to the server using AJAX, and load the data into the second dropdown based on the selected value.
Rate this post
5 of 5 based on 4310 votesComments