mercredi 21 novembre 2018

dynamic population of a select list from a text file

I have a website that allows the user to query a database by selecting values in three dropdowns.

Currently, I populate the select list using a single text file. However, there is a design change and now we want to populate the second drop-down using the entry selected in the first drop-down. I know it is not very uncommon to do this but I am very new to website design and need some guidance. I have not come across any example which uses just a simple text file and reads it the way I am currently doing.

So, when a user selects an Application from the first select list, the corresponding configuration should get loaded in the second select box. For every application, I have a configuration file associated with it e.g app1_config.txt, app2_config.txt and so on. I know, I have to use some event listener and then may be used a switch case to do mapping of app name to correct config file and then read the file the same way I am doing now. Can somebody please guide me on this. Most of the examples I saw online were using ajax/json/database etc. Can I really implement the desired dynamic loading with minimal changes to my current approach? I really need to use all the text files for populating the select list.

e.g. App1_config.txt will have entries like:

config1
config2
config3
config4 

Here is what my php code looks like:

<body>
<div id = header>
<div class = container> Database Query </div>
</div>
<div class = container>
<form action="""" method = "post">
//populating first dropdown using text file
<?php
 $filename='/path/to/the/text/file.txt';
 $eachlines =  file($filename, FILE_IGNORE_NEW_LINES);
?>
 <div id = param1>
 <p align="top"><b> Application: </b></p>

<select size=15 name = "App" required>
  <?php foreach($eachlines as $lines){ //add php code here
            echo "<option value='".$lines."'>$lines</option>";
        }?>
</select>
</p>
</div>
<?php
 $filename='db_report_cfg_string.txt';
 $eachlines =  file($filename, FILE_IGNORE_NEW_LINES);
?>
<div id = param2>
<p align="top"><b> Configuration: </b></p>

<select size=15 name = "Config" required>
  <?php foreach($eachlines as $lines){ //add php code here
            echo "<option value='".$lines."'>$lines</option>";
        }?>
</select>
</p>
</div>
//some code to populate another select list and provide email address (not shown here)


<?php
if(isset($_POST['submit'])){
  $varApp= $_POST['App'];
  $varConfig = $_POST['Config'];
  $varCtrType = $_POST['CtrType'];
  $varEmail = $_POST['mailid'];
//some processing based on values provided by user. 
exec("/py $varApp $varConfig $varCtrType 2>&1",$output );
if ($output[8] == "Empty"){
echo "<div style ='font:22px Arial,tahoma,sans-serif;color:#ff0000'><br>No Data Available! <br></div>";
}
else {
     exec(' printf "Please find attached the query result for following selection:\n\nApp: '.$varApp.'  \nConfig: '.$varConfig.' \nCounter Type: '.$varCtrType.' \n\n Thanks! "  | /bin/mail -s "Database Query Result" -a '.$output[8].' '.$varEmail.'  2>&1', $output2 ); 
echo "<div style ='font: 18px Arial,tahoma,sans-serif;color:#10ac84'><br><b> Please check your email for result !<b> <br>";
echo '<script language="javascript">';
echo 'alert("Please check your email for result! Submitted Query details: Selected App: '.$varAPP.' Configuration:")';
echo '</script>';
}
$_POST=array();

}
?>


</body>

I deeply appreciate your guidance here.




Aucun commentaire:

Enregistrer un commentaire