Echo data values from txt file based on selection click

I need to echo data values from column 1 - 3 based on selection in dropdown list which is taken from same txt file column 0

<select style="width: 120px;" name="mylist" id="mylist" required="">
            <option selected="selected" value="">Select Your Company</option>
           <?php $lines = file('customers.txt');
               foreach($lines as $line){
               $customers = explode(',', $line);
                echo "<option value='".$customers[0]."'>$customers[0] </option>";
            }?>
</select>

and html where I need to display (echo selected result)

<textarea id="customer-title">
echo column 1 (based on selection in dropdown list)
echo column 2 (based on selection in dropdown list)
echo column 3 (based on selection in dropdown list)
</textarea>

Well, assuming you’re submitting the form, your value in PHP will be sitting in either $_POST['mylist'] or $_GET['mylist'], depending on how you’re submitting it.

I don’t submit via form I only use for selection and based on that i want to echo values

Then I think you’ll need to use some Javascript to capture when the <select> is chosen, and store the three columns in the value of each <option>. The Javascript can extract the values and put them in the appropriate location on the page.

Thanx this put me in the right direction - give a class to select and use in my js, but still need a little help with displaying column 1-3 values

<select class="select" style="width: 250px;" name="mylist" id="mylist" required="">
        <option selected="selected" value="">Select Your Company</option>
        <?php foreach($lines as $line){
       $customers = explode(',', $line);
        echo "<option value='".$customers[0]."'>$customers[0] </option>"; }?>
</select>

<input type="text" id="inputid" placeholder="Company Name">
<script>
    $(".select").change(function(){
  var res = $(".select").find(":selected").map(function () {
    if($(this).val()!="")
      return $(this).text();
    else
      return "";
   }).get().join(" ");
  
   $("#inputid").val(res);

 });
</script>

From the look of that, you’ve ‘borrowed’ some code from elsewhere for a multselect box.

Won’t break anything, but it’s a sign.

This would be my suggestion at this stage;

Have the PHP generate (echo out) a javascript object, such that the property keys are the option values from the select box, and the property values are the strings you want to appear in the textarea.
Alter the return of your res function to return the property of the object that was selected by the select box.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.