0

I'm not sure how to tackle this one, so I need some help.
I have 3 select boxes, and each select box has its content from text file.

  1. select1 list from customer.txt
  2. select2 list from select1 selection + "_location.txt"
  3. select3 list from select1 selection + select2 selection + "_device.txt"

select 2 will update when select1 change, and select3 update when select2 change

Please help how to make it work?

This is my code:

        <table width=100% border="0" cellpadding="1" >
    <tr>
    <td>
    <label class="description" for="element_21">Customer </label>
    <div>
    <select name="select1" id="select1" >
        <option selected value="base">Choose Customer</option>
       <?php foreach($customerlines as $custlines){ 
            echo "<option value='".$custlines."'>$custlines</option>";
            $lokasi = "config/".$custlines."_lokasi.txt";   //txt file for select2 list
            $lokasilines = file($lokasi, FILE_IGNORE_NEW_LINES);
            }?>
    </select>
    </div>
    </td>
    <td>
    <label class="description" for="element_21">Location</label>
    <div>
    <select name="select2" id="select2">
        <option selected value="base">Choose Location</option>
       <?php
       foreach($lokasilines as $loclines){ 
            echo "<option value='".$loclines."'>$loclines</option>";
            $device = "config/"."_".$loclines."_device.txt";    //txt file for select3 list
            $devicelines = file($device, FILE_IGNORE_NEW_LINES);

            }?>
    </select>
    </div>
    </td>
    <td>
    <label class="description" for="element_21">Device</label>
    <div>
    <select name="select3" id="select3">
        <option selected value="base">Choose Device</option>
       <?php 
        foreach($devicelines as $devlines){ 
        echo "<option value='".$devlines."'>$devlines</option>";        
        }?>
    </select>
    </div>
    </td>
    </tr>
    </table>

and this is txt file for select1

<?php
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    $customer = 'config/customer.txt';
    $customerlines = file($customer, FILE_IGNORE_NEW_LINES);
?>

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • For this you will need to use javascript. PHP alone is not enough as it is a server side language. Here, read this tutorial https://www.w3schools.com/jsref/event_onchange.asp – Chilarai Aug 02 '20 at 11:18
  • Thank you Chilarai. Yes i try use onchange , but my second select still not update. i want to change the txt file for second select by the value of select1. – choco_nanas Aug 04 '20 at 02:28
  • you need to post your javascript code as well – Chilarai Aug 04 '20 at 02:44

1 Answers1

0

i add onchange at select1

    <br>
    
    <table width=100% border="0" cellpadding="1" >
    <tr>
    <td>
    <div class='form-group'>
    <label class="description" for="element_21">Customer </label>
    <select class='form-control' name="select1" id="select1" onchange="selectloc(event)" >
        <option selected value="base">Pilih Customer</option>
       <?php foreach($customerlines as $custlines){ 
            echo "<option value='".$custlines."'>$custlines</option>";
            }?>
    </select>
    </div>
    </td>
    <td>
    <div class='form-group'>
    <label class="description" for="element_21">Lokasi</label>
    <select class='form-control' name="select2" id="select2" >
        <option selected value="base">Pilih Lokasi</option>
       <?php
       foreach($lokasilines as $loclines){ 
        echo "<option value='".$loclines."'>$loclines</option>";
        }?>
    </select>
    </div>
    </td>
    <td>
    <div class='form-group'>
    <label class="description" for="element_21">Perangkat</label>
    <select class='form-control' name="select3" id="select3" >
        <option selected value="base">Pilih Perangkat</option>
       <?php
        foreach($devicelines as $devlines){ 
        echo "<option value='".$devlines."'>$devlines</option>";        
        }?>
    </select>
    </div>
    </td>
    </tr>
    </table>

and i can get the value of select1. But how to use this value to process into text file for select2 ?

<script>
function selectloc(event) {

    var selectElement = event.target;
    var locvalue = selectElement.value;
     //"You selected: " + locvalue;
    //document.getElementById("select1").innerHTML = "You selected: " + locvalue;
    alert(locvalue);
    $lokasi = "config/".locvalue."_lokasi.txt";
    $lokasilines = file($lokasi, FILE_IGNORE_NEW_LINES);
    
}</script>