Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda