0

im trying to get some data from a DropDown menu and i want to use that value when it change to make a Query in a secound DropDown but until now, i was unable to send the Value to my php with the javascript.

Im using this code:

<script type="text/javascript" language="javaScript">

  function flow() {
    
    var x = document.getElementById("txt_maq").value;
    document.getElementById("demo").innerHTML = x;
    
    var maq = document.getElementById('txt_maq').value;
    var cat = document.getElementById('txt_catmaterial').value;

    if (maq != "")
    {
      document.getElementById('txt_catmaterial').disabled=false;
    }
    if (cat == "")
    {
      document.getElementById('txt_material').disabled=true;
    }
    if (cat != "")
    {
      document.getElementById('txt_material').disabled=false;
    }
                
                
  }

</script>
<div class="col-2">
      <div class="input-group">
        <label class="label">Maq. Destino</label>
        <div class="rs-select2 js-select-simple select--no-search">
          <select id="txt_maq" name="txt_maq" onchange="flow()">
            <option value="">Selecione</option>
            <option value="E02">E02</option>
            <option value="E03">E03</option>
            <option value="E04">E04</option>
            <option value="E05">E05</option>
            <option value="E06">E06</option>
            <option value="E07">E07</option>
            <option value="E08">E08</option>
            <option value="E04/E07">E04/E07</option>
            <option value="E04/E08">E04/E08</option>
            <option value="E03/E04">E03/E04</option>
            <option value="E03/E07">E03/E07</option>
            <option value="E04/E07/E08">E04/E07/E08</option>
            <option value="E03/E07/E08">E03/E07/E08</option>
            <option value="E07/E08">E07/E08</option>
            <option value="E09">E09</option>
          </select>
          <div class="select-dropdown"></div>
          </div>    
        </div>
      </div>
</div>

<div class="row row-space">
  <div class="col-2">
    <div class="input-group">
      <label class="label">Cat. Material</label>
      <div class="rs-select2 js-select-simple select--no-search">

        <select id="txt_catmaterial" disabled="true" name="txt_catmaterial" onchange="flow()">
            <option value=""></option>
            <?php

              $maquina = $_POST['txt_maquina'];
              $type_te = "";


              if ($maquina == "E09"){
                $type_te= "ET";
              } else {
                $type_te= "EP";
              }
              
              echo $type_te;
              $selecione = "";
              $consulta = "SELECT Cat_Material FROM cat_mat where TE = '".$type_te."' "; //código SQL
                    


                $resultado = mysqli_query($ligacao,$consulta);

                while ($listar = mysqli_fetch_array($resultado)){

                    $catmat = $listar['Cat_Material'];
                    
                    echo "<option value=".$catmat.">$catmat</option>";
                }
            ?>                                          
        </select>   
                    
        <div class="select-dropdown"></div>
      </div>        
    </div>
  </div>
</div>

All I try to do is make a If if the txt_maq is E09 so I can do the query in the txt_catmaterial but the value doesn't come from the javascript.

a.ak
  • 659
  • 2
  • 12
  • 26
  • You are using a wrong POST index `$maquina = $_POST['txt_maquina'];` In your form, the name of the select element is `txt_maq` In your php code, it should be `$maquina = $_POST['txt_maq'];` – Raahim Fareed May 20 '21 at 08:04
  • Selecting any value from the dropdown != posting. So you need to catch this event with javascript/jquery, then use ajax to make request to your DB last populate your 2nd select with the values from the response – Angel Deykov May 20 '21 at 08:06
  • Also, like Angel and peter have mentioned, you will need to use either ajax/fetch to make it work or you will need the form to actually send the data. PHP is a server side language, it has no idea what is happening on the client side (ie. JS). This link might help you get started, possible duplicate: https://stackoverflow.com/questions/1917576/how-do-i-pass-javascript-variables-to-php – Raahim Fareed May 20 '21 at 08:07

2 Answers2

0

Use Ajax to send data from Javascript to PHP


function testEndpoint(){
        $.ajax({
            url: "https://path/to/your-php-script.php",
            type: "POST",
            crossDomain: true,
            data: {"txt_maq": $("#txt_maq").val()},
            dataType: "json",
            error: function(xhr, status, error){
                console.log("Error: " + error);
            },
            success: function(result, status, xhr){
                console.log(result);
            }
        });
}

testEndpoint();

At your PHP script your-php-script.php You can use txt_maq as a POST variable:

$useTxtMag = $_POST['txt_maq'];

Peter
  • 60
  • 8
-1

The Ajax should be used after the jQuery is included or you can simply include the jQuery in your head element

Peter
  • 60
  • 8
  • I include the jQuery in my head element and still dont work, always the same error Warning: Undefined array key "txt_maq" in C:\xampp\htdocs\san\materias_primas\novo_registo.php on line 131 – Diogo Rocha May 20 '21 at 09:51
  • You should only call the Ajax function onchange of the txt_maq field – Peter May 20 '21 at 09:55
  • Im provably doing sometging wrong because i follow every step that you told me and still dont work can you please check this link from from pastebin to see where is my problem here please? https://pastebin.com/ULDdSaNJ – Diogo Rocha May 20 '21 at 10:01