Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda