First of all, im completely new to PHP and coding, but here it goes.
I have a web form that should get some information from a txt file (located in a folder on the server) I got how to get it to get information from a file and how to make a drop-down list, show all the files in the folder. Be that as it may, I cannot figure out how to make them work together, so the name of the file from which it is read is taken from the selected value of the drop-down list.
code for dropdown:
<? $currentdir = 'files'; //change to your directory $dir = opendir($currentdir); echo 'Files are as follows:<br>'; echo '<select name="select">'; while($file = readdir($dir)) { echo '<option value="'.$file.'">'.$file.'</option>'; } echo '</select>'; closedir($dir); ?>
and code to read from a file:
<input type="text" value="<?php $myFile = ""; $lines = file($myFile); echo $lines[2]; ?>" name="refnr" id="refnr" class="input" />
lets say that 2 files are shown in the drop-down list, test.txt and test2.txt, if I select test2.txt from the drop-down list, I wanted it to put "test2.txt" between "" on <?php $myFile = "" ; but no matter how I try to put the code there to get the selected value, it just fails ...
The full code of my form, as of now:
<?php if (!empty($_POST)) { <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Felanmälan från IKEA</title> <style type="text/css"> html, body, h1, h2, h3, h4, h5, h6, p, span, ul, li, div, form, input, select, textarea, button {margin:0; padding:0;} ul {list-style:none;} a, a:hover {text-decoration:none; outline:0;} a img {border:0;} body {font:12px/16px Verdana, Arial, sans-serif; background:#ffffff;} #container {width:450px; margin:10px auto; padding:10px; overflow:hidden; border:1px solid #000; border-radius:10px; -moz-border-radius:10px; -webkit-border-radius:10px; background:#F9F9F9;} #container h1 {margin-bottom:20px; font-size:40px; line-height:40px; font-family:'HelveticaNeue-Light', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight:normal;} .message {margin-bottom:10px; padding:5px;} .success {color:#4F8A10; border:1px solid #4F8A10; background:#DFF2BF;} .error {color:#D8000C; border:1px solid #D8000C; background:#FFBABA;} label {display:block; margin-bottom:3px; cursor:pointer;} .input, textarea, select, button {display:block; width:440px; margin-bottom:10px; padding:3px; font:22px/22px 'HelveticaNeue-Light', 'Helvetica Neue', Helvetica, Arial, sans-serif; border:1px solid #CCC; border-top-width:2px;} textarea {font-size:13px; line-height:16px;} select {width:396px;} button {float:right; width:auto; margin-bottom:0; padding:3px 30px; cursor:pointer; font-size:16px; border:1px solid #999; border-bottom-width:2px; border-radius:5px; -moz-border-radius:5px; -webkit-border-radius:5px; background:#EEE;} button:active {border-bottom-width:1px; padding:4px 30px 3px; background:#E9E9E9;} </style> </head> <body> <? $currentdir = 'files'; <div id="container"> <h1><img src="felimg.png" /> Felanmälan</h1> <?php if ($success) { ?> <div class="message success"> <h4>Congratulations! It worked! Now check your email.</h4> </div> <?php } elseif ($error) { ?> <div class="message error"> <h4>Sorry, an error occurred. Try again!</h4> </div> <?php } ?> <form method="post" action=""> <label for="date"><b>Date:</b></label> <input type="text" readonly name="date" id="date" class="input" value="<? print(Date("Ymd")); ?>"/> <label for="refnr"><b>Referensnummer:</b></label> <input type="text" value="<?php $myFile = ""; $lines = file($myFile); echo $lines[2]; ?>" name="refnr" id="refnr" class="input" /> <label for="bestav"><b>Beställd av:</b></label> <input type="text" name="bestav" id="bestav" class="input" /> <label for="tel"><b>Tel:</b></label> <input type="text" name="tel" id="tel" class="input" /> <label for="email"><b>Email:</b></label> <input type="text" name="email" id="email" class="input" /> <label for="kund"><b>Kund:</b></label> <textarea name="kund" id="kund" rows="4" cols="40"></textarea> <label for="ktel"><b>Tel:</b></label> <input type="text" name="ktel" id="ktel" class="input" /> <label for="art"><b>Berörd Artikel:</b></label> <textarea name="art" id="art" rows="3" cols="40"></textarea> <label for="fel"><b>Fel på varan: </b></label> <textarea name="fel" id="fel" rows="2" cols="40"></textarea> <label for="q1"><b>Installation gjord av fackman:</b></label> <select name="q1" id="q1"> <option value="Ja">Ja</option> <option value="Nej">Nej</option> </select> <label for="q2"><b>Serviceverkstad:</b></label> <input type="text" name="q2" id="q2" class="input" /> <label for="q3"><b>Servicenr:</b></label> <input type="text" name="q3" id="q3" class="input" /> <label for="q4"><b>Serienr:</b></label> <input type="text" name="q4" id="q4" class="input" /> <label for="q5"><b>Inom garanti eller reklamation:</b></label> <select name="q5" id="q5"> <option value="Garanti">Garanti</option> <option value="Reklamation">Reklamation</option> </select> <label for="q6"><b>Informerat om punkt 8:</b></label> <select name="q6" id="q6"> <option value="Ja">Ja</option> <option value="Nej">Nej</option> </select> <label for="q7"><b>Har kund själv gått igenom manual för felsökning:</b></label> <select name="q7" id="q7"> <option value="Ja">Ja</option> <option value="Nej">Nej</option> </select> <label for="q8"><b>Ordernr:</b></label> <input type="text" name="q8" id="q8" class="input" /> <label for="q9"><b>Inköpsdatum:</b></label> <input type="date" name="q9" id="q9" class="input" /><br> <p><button type="submit">Submit!</button></p> </form> </div> </body> </html>
Hope this makes sense and again, sorry if that would be a question about the noob :) I used 2 days of searching and testing, and I just went to the point where I need to push a little in the right direction.
Thanks in advance.
ps, to explain what my goal is, then I have 1 web form that generates a txt file and saves it on the server, and then another form where the “administrator” can select the file from the drop-down list, get the information that the client fills in the txt version, and then fills in what it needs to fill out, and then save and send to PDF as a specialist.