0

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 .............................................................

1 Answers1

0

Try:

onchange="getdll(this.value)"

THEN in your script, put a parameter


function getdll(val){
   alert(val);
}

Should be able to get the value you selected

tmarois
  • 2,424
  • 2
  • 31
  • 43
kelvin
  • 1
  • 1
  • i already getting the value but i cant load it to the query $hall_qry = $conn->query("SELECT * FROM `assembly_hall` WHERE o_id = '$element' order by `room_name` asc"); – Jonjon Candare Aug 04 '22 at 03:16
  • So what you want is to get the first Select Option value and put it inside the second Select Option query? – kelvin Aug 04 '22 at 03:27
  • And you can't use $element in script, this is not php – kelvin Aug 04 '22 at 03:32
  • Try use ajax post method, so that you can get the value and assigned it to PHP variable, then put this variable into your query – kelvin Aug 04 '22 at 03:33
  • @kelvin _"And you can't use $element in script, this is not php"_ - JavaScript has no problem with a dollar sign as part of a variable name. After all, the whole of jQuery uses `$` as variable name for its main instance :-) – CBroe Aug 04 '22 at 06:44
  • _"i already getting the value but i cant load it to the query"_ - you will need to send this value back to the server, before you can use it in a query _on_ the server side of your coding ... – CBroe Aug 04 '22 at 06:45
  • You should go read [What is the difference between client-side and server-side programming?](https://stackoverflow.com/q/13840429/1427878), because it appears you lack some of the basics here ... – CBroe Aug 04 '22 at 06:46