0

I have the selection menu which takes the value from the txt file. I want the selected value to remain selected even after the form submission.

<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>

<form action="#" method="post">
    <select id="toolchain" name="toolchain" onchange='this.form.submit()'>
         <option selected value="base">Please Select</option>
          <?php foreach($eachlines as $lines){ 
                echo "<option value='".$lines."'>$lines</option>";
          }?>
    </select>
</form>

For keeping the value selected,I tried this:

<?php foreach($eachlines as $lines){ 
   echo "<option value='".$lines."'" if($_POST['$lines']) echo $_POST['$lines'];">$lines</option>";
}?>

But it is not working, may be I am using echo inside the echo. Please correct me.

Ratnesh
  • 1,554
  • 3
  • 12
  • 28
  • There is no benefit in repeating the options' text as their `value` attribute. You can safely rely on the text for all submission and javascript purposes -- nothing will break. Keep your HTML document lean and clean. – mickmackusa Oct 29 '22 at 10:40

1 Answers1

1

You can use this code. Although I haven't tested the code but let me share my logic with you to make you a better understanding of it. I have added a condition that if the form is submitted then it should display the form with $_POST['toolchain'] selected otherwise it should display it in normal ways.

<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
if(isset($_POST['toolchain'])
{
 ?>
  <form action="#" method="post">
  <select id="toolchain" name="toolchain" onchange='this.form.submit()'>
    <option selected value="<?php echo $_POST['toolchain']; ?>"><?php echo $_POST['toolchain']; ?></option>
     <?php foreach($eachlines as $lines){ 
       if($lines!=$_POST['toolchain'])
        echo "<option value='".$lines."'>$lines</option>";
     }?>
  </select>

 </form>
 <?php }
else{
?>

<form action="#" method="post">
 <select id="toolchain" name="toolchain" onchange='this.form.submit()'>
     <option selected value="base">Please Select</option>
      <?php foreach($eachlines as $lines){ 
            echo "<option value='".$lines."'>$lines</option>";
      }?>
 </select>

</form>
 <?php }
 ?>

It's a normal but a lengthy way. You can also add a condition inside form. If the form is submitted mean if(isset($_POST['toolchain'])) then you can make the option selected for the $_POST['toolchain'] inside foreach loop

Update

I have put the condition inside the form. Kindly update if you face any error as I haven't tested the code

    <?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form action="#" method="post">
 <select id="toolchain" name="toolchain" onchange='this.form.submit()'>
   <?php if(isset($_POST['toolchain']))
      {
        ?>
        <option value="base">Please Select</option>
        <?php foreach($eachlines as $lines){
            if($_POST['toolchain']==$lines))
            {
              echo "<option selected value='".$lines."'>$lines</option>";
            }
            else {
              echo "<option value='".$lines."'>$lines</option>";
            }
      }
   }
   else {

   ?>
     <option selected value="base">Please Select</option>
      <?php foreach($eachlines as $lines){
            echo "<option value='".$lines."'>$lines</option>";
      }
    }
    ?>
 </select>

</form>
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42