2

I have an option menu (selFilter) that filters a MYSQL query. The option menu choice is used in a link to filter a second PHP page. export.php?recordID=

<select name="selFilter" id="selFilter" onchange="formFilter.submit()">
    <option value="%">all levels</option>
    <?php
do {  
?>
    <option value="<?php echo $row_RecordsetLevel['Level']?>"<?php if   
($varFilter_Recordset3 == $row_RecordsetLevel['Level']) {echo 'selected';}   
?>>level: <?php echo $row_RecordsetLevel['Level']?></option>
    <?php
} while ($row_RecordsetLevel = mysql_fetch_assoc($RecordsetLevel));
$rows = mysql_num_rows($RecordsetLevel);
if($rows > 0) {
  mysql_data_seek($RecordsetLevel, 0);
  $row_RecordsetLevel = mysql_fetch_assoc($RecordsetLevel);
 }
 ?>
  </select>

The second php page uses a query to export to .csv using the following:

$colname = "%";
if (isset($_GET['recordID'])) {
$colname = $_GET['recordID'];
}


$query = sprintf( 'SELECT Name, ID
FROM schedule WHERE Level LIKE %s ORDER BY Name', 
GetSQLValueString("%".$colname."%", "text"));

$result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) );
//
// send response headers to the browser
// following headers instruct the browser to treat the data as a csv file 
called export.csv

//
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename=export.csv' );

Everything works fine when an option (selFilter) is chosen in the index.php file and I click export.php. The problem is if I don't pick anything in the option menu and click export.php link, the URL string is then export.php?recordID= with recordID being blank. That blank causes nothing to export, zero records. If I select an option menu level and then click all levels the URL is export.php?recordID=% and exports correctly. What should I fix so I can export from either call?

I have tried the following but export URL is still blank:

export.php?recordID=<?php $filter=$_POST['selFilter']; if (!empty($filter)) { echo "%"; } ?>
user3258571
  • 386
  • 3
  • 17

1 Answers1

0

I found a solution. Changed export link to:

 export.php?recordID=<?php 
$filter=isset($_POST['selFilter']) ? $_POST['selFilter'] : '%';
 echo "/export_student.php?recordID=$filter";
 ?>

Used solution from stackoverflow: PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

I just substituted

$filter=isset($_POST['selFilter']) ? $_POST['selFilter'] : '';

with

 $filter=isset($_POST['selFilter']) ? $_POST['selFilter'] : '%';

since my query was triggering an undefined index based on my option menu did not set a value.

Community
  • 1
  • 1
user3258571
  • 386
  • 3
  • 17