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

142
Visualizações
Get a value out of a php array in a js function

I'm pretty new to this. I'm trying the following. I have a select input dropdown in the productionCalculator.php. Whenever it's input changes, I call a js function. This function needs an array out of a php file. I already learned a lot (like I have to use include unlike require f.e.) but I the only "value" I get back is null. When I define a string variable directly in the called php file, it works, but if I access the array in the other php the returned value is null. And I can't call the array php file directly, because echo would write the result on the site. I'm really stuck. Pls help, I thank you so much.

There a 4 files involved:

The selection in the productionCalculator.php

<body>
        <img src="https://images.evetech.net/types/1002/bp" id="bpoImage">
            <select name="bpoDropdown" id="bpoDrop" onchange="SetBpoImage()">
            <?php 
            foreach ($blueprintDict as $key => $value) { ?>                
                <option value=<?php echo $key?>><?php echo $key?></option>
                <?php 
            }
        ?>
        </select>
    </body>

The js function which is called, when the dropdown changes. This is a .js file

function SetBpoImage()
{
    var e = document.getElementById("bpoDrop");
    var index = e.selectedIndex;

    var req = new XMLHttpRequest(); 
    req.onload = function() {
    console.log(this.responseText); 
    };
    req.open("get", "assets/getBlueprintDict.php", true); 
    req.send();
}

The getBlueprintDict.php

<?php
require("assets/blueprintDict.php");
echo json_encode($blueprintDict); 
?>

And the blueprintDict.php I need in the js function.

<?php 
$blueprintDict=array(
"Typhoon" => 1,
"Dominix" => 2,
"Erebus" => 3,
"Small Shield Extender I" => 4,
"Survey Scanner I" => 5,
);
?>

Folder Structure: main folder main folder

assets folder assets folder

about 4 years ago · Juan Pablo Isaza
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