Pass php drop down value to another php site

243 Views Asked by At

I am new to php, and would like to pass a drop down menu value to another php file. Below is the code I have pieced together so far.

Drop down php (Lists all files in a directory, although i'd like to omit the folders and the "." & ".."):

 <?php
$currentdir = 'data/';
$dir = opendir($currentdir);
echo 'Files are as follows:<br>';
echo '<form action="SelectedDate.php" method="POST">';
echo '<select name="selectedfile" onchange="this.form.submit()">';
while($file = readdir($dir))
{
    echo '<option value="'.$file.'">'.$file.'</option>';
}
echo '</select>';
echo '</form>';
closedir($dir);
?>    

Table generating php, which is where i'd like to pass the file name too so it can pull up the selected csv file,formatted to table. :

<?php
$var=$_REQUEST['selectedfile'];
$row = 1;
if (($handle = fopen('"data/"'$var'"', "r")) !== FALSE) {
    echo '<head>';
    echo '<script src="js/sorttable.js"></script>';
    echo '</head>';
    echo '<table class="sortable" border="1">';
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        if ($row == 1) {
            echo '<thead><tr>';
        }else{
            echo '<tr>';
        }  
        for ($c=0; $c < $num; $c++) {
            //echo $data[$c] . "<br />\n";
            if(empty($data[$c])) {
               $value = "&nbsp;";
            }else{
               $value = $data[$c];
            }
            if ($row == 1) {
                echo '<th>'.$value.'</th>';
            }else{
                echo '<td>'.$value.'</td>';
            }
        }

        if ($row == 1) {
            echo '</tr></thead><tbody>';
        }else{
            echo '</tr>';
        }
        $row++;
    }

    echo '</tbody></table>';
    fclose($handle);
}
?>

I cant seem to get the drop down value to pass into the fopen() function...

Any help and critiquing would be great!

2

There are 2 best solutions below

7
On BEST ANSWER

For readability I would prefer $_POST instead of $_REQUEST, besides that you have an error:

(($handle = fopen('"data/"'$var'"', "r"))
   //SHOULD BE:
(($handle = fopen("data/".$var.", "r"))

======

Updated answer:

Make sure your files are both saved with an .php extension. Besides that, add the following lines of code to the top of your second file:

<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

As a finishing try, use this code in the if

(($handle = fopen("data/{$var}", "r"))
0
On

Change this line:

if (($handle = fopen('"data/"'$var'"', "r")) !== FALSE) {

Into

if (($handle = fopen('"data/"'.$var.'"', "r")) !== FALSE) {

Note the dots!