To make this work you will need to have the data in the select
box to be posted to the PHP file.
HTML code in theForm.php
:
<form action="someFile.php" method="POST">
<select name="car" id="car">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit value="Go"/>
</form>
This tells the browser that when the user click "Go", to send the data from the drop-down box to someFile.php
where it can be used by PHP.
PHP code in someFile.php
:
<?php
$file = $_POST['car'] . '.txt';
$contents = file($file);
$string = implode("<br>", $contents);
echo $string;
?>
The $_POST
array is a built-in PHP variable containing information sent to the server from a form. The PHP then uses that text to figure out which .txt
file should be read. That file is then read from the file and echo
ed to the user.
So if the user selected Volvo
, this code will return the contents of a file named volvo.txt
.