Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
Variable assigned to HTML input's value through PHP is not understood by JS script

I'm working on a project of a website which shows a chart. User should be able to change a displayed chart (without changing the website) by clicking one of 'Available sensors' from dropdown options. Dropdown connects to MySQL database with used sensors. The sensor's id is assigned to HTML-input ID and its name is assigned to input value.

My intension is to use sensor ID in another data.php file which is responsible for connecting to tables (MySQL) with data collected by sensors. This ID would tell to which of the tables this programm should connect.

At the moment JS script's task is to alert an ID of the chosen sensor when it's clicked on the dropdown menu. Instead of a number I get a message saying 'undefined'. Eventually it would transfer the stored id to the mentioned data.php file.

Could you please tell me whether it's necessary to use AJAX in this case or what's a possible reason of this error in my code?

I also tried to use button insted of input. When clicking on sensors names on dropdown I've received only messages with '1'. However assigning sensorName worked out in both cases. Sensors ID is stored as INT, name as VARCHAR in MySQL table.

Thank you in advance for your help :)

<div id="header_btn" class="dropdown">

    <input type="submit" id="btn" value="Available sensors" class="btn btn-success" />

    <div class="dropdown-content">
        <?php
            include("config.php");
            $sql = "SELECT * FROM sensors";
            $result = $db->query($sql);

            if($result->num_rows > 0){
                while($row = $result->fetch_assoc()){
                    $sensorID = $row["id"];
                    $sensorName = $row["WebName"];
        ?>
                <input onclick="changeSensorID(this.value)" onmouseover="this.style.textDecoration='underline'" onmouseout="this.style.textDecoration='none'" class="btn_drop" id="<?php echo $sensorID ?>" value="<?php echo $sensorName ?>" /></a>
        <?php
                }
            }
        ?>

    </div>
    <script>
        function changeSensorID() {
            var sensorID = document.getElementsByClassName("btn_drop").id;
            alert(sensorID);
        };
    </script>
</div>
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

please check this code, working fine

            <input type="submit" id="btn" value="Available sensors" class="btn btn-success" />

            <div class="dropdown-content">
                <?php
                    include("config.php");
                    $sql = "SELECT * FROM sensors";
                    $result = $db->query($sql);

                    if($result->num_rows > 0){
                        while($row = $result->fetch_assoc()){
                            $sensorID = $row["id"];
                            $sensorName = $row["WebName"];
                ?><input onclick="changeSensorID(event)" onmouseover="this.style.textDecoration='underline'"
                                onmouseout="this.style.textDecoration='none'" class="btn_drop" id="<?php echo $sensorID ?>"
                                value="<?php echo $sensorName ?>" /></a>


                        <?php
                        }
                    }
                        ?>

            </div>
           <script >
                function changeSensorID(event){
                    var sensorID = event.target.id;
                    alert(sensorID);
                }
            </script>


        </div>
about 4 years ago · Santiago Trujillo Report

0

getElementsByClassName returns array of at least one item if found any. You have to provide index of element that you want to use.

Example

var sensorID = document.getElementsByClassName("btn_drop")[0].id;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!