Forum Moderators: coopster
<HTML>
<head><title>SEARCH TOOL - PROTOTYPE</title></head>
<body><h1>SEARCH TOOL - PROTOTYPE</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Filename (one item)</legend><select name="DBFilename" id="DBFilename" size="0">
<?php $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die(mysql_error());
$result = mysql_query("select distinct filename from search_test");
while ($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option> <?php } mysql_close($con); ?>
</select></fieldset>
<fieldset>
<legend>Site (one item)</legend><select name="DBSite" id="DBSite">
<?php $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die(mysql_error());
$result = mysql_query("select distinct site from search_test");
while ($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['site']; ?>"><?php echo $row['site']; ?></option> <?php } mysql_close($con);
?>
</select></fieldset>
<input type="submit" name="submit" value="submit" >
<input type="button" value="Reset Form" onClick="this.form.reset();return false;" />
</form>
</body>
</HTML>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['DBFilename'])) {doFileSearch();}
elseif (!empty($_POST['DBSite'])) {doSite();}
}
function doFileSearch() {
$mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot";
$dir = opendir($mydir);
$DBFilename = $_POST['DBFilename'];
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die("Couldn't select the database.");
$getfilename = mysql_query("select filename from search_test where filename='" . $DBFilename . "'") or die(mysql_error());
echo "<table><tbody><tr><td>Results.</td></tr>";
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
echo '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>';
}
echo "</table></body>";
}
function doSite() {
$mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot";
$dir = opendir($mydir);
$DBSite = $_POST['DBSite'];
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die("Couldn't select the database.");
$getfilename = mysql_query("select distinct filename from search_test where site='" . $DBSite . "'") or die(mysql_error());
echo "<table><tbody><tr><td>Results.</td></tr>";
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
echo '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>';
}
echo "</table></body>";
}
?>
whatever option I select from the second select list is never captured when I hit Submit.
However, whatever option I select from the first select list is always captured.
<HTML>
<head><title>SEARCH TOOL - PROTOTYPE</title></head>
<body>
<h1>SEARCH TOOL - PROTOTYPE</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
// Since this is CONSTANT - doesn't change - set it in one place
define(MYDIR,$_SERVER['DOCUMENT_ROOT'] . "/filedepot");
// Make one global connection
$con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die(mysql_error());
// Check for search input
list ($searchcontent,$err) = check_search();
//
// If there's any error, output it here, at the top
if ($err) { echo "<p style=\"font-weight:bold;color:#ff0000;\">$err</p>"; }
// Now continue with the form
?>
<fieldset>
<label for="DBFilename">Filename (one item)</label>
<select name="DBFilename" id="DBFilename" size="0">
<option value="">Select Filename</option>
<?php
$result = mysql_query("select distinct filename from search_test");
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option>
<?php } ?>
</select>
</fieldset>
<fieldset>
<label for="DBSite">Site (one item)</label><select name="DBSite" id="DBSite">
<option value="">Select Site</option>
<?php
$result = mysql_query("select distinct site from search_test");
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['site']; ?>"><?php echo $row['site']; ?></option>
<?php } ?>
</select></fieldset>
<input type="submit" name="submit" value="submit">
<!-- button and JS not needed unless you're doing something besides "reset"
reset resets form to original loaded state, which is not the same as "clear the form" -->
<input type="reset" value="Reset Form">
</form>
<?php
// Note $results is NULL by default in check_search.
if ($searchcontent) { echo $searchcontent; }
// NOW close - if it's open
if ($con) {mysql_close($con); }
?>
</body>
</HTML>
<?php
//
function check_search() {
$errors = $results = null;
if (isset($_POST['submit'])) {
if (!empty($_POST['DBFilename'])) { $results = doFileSearch(); }
elseif (!empty($_POST['DBSite'])) { $results = doSite(); }
else ($errors = "Please select a site search or a file search."; }
}
return Array($results, $errors);
}
//
function doFileSearch() {
$result=null;
if (empty($_POST['DBFilename'])) { return null; }
$mydir = MYDIR; Set from the CONSTANT
$dir = opendir($mydir);
$DBFilename = $_POST['DBFilename'];
$getfilename = mysql_query("select filename from search_test where filename='" . $DBFilename . "'") or die(mysql_error());
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
$result .= '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>';
}
if ($result) {
$result = "<table><tbody><tr><td>FILE Results.</td></tr> $result</table>";
}
return $result;
}
//
function doSite() {
// Note that this "$result" is constrained to
// THIS function and doesn't affect the other $result above
$result = null;
if (empty($_POST['DBSite'])) { return null; }
$mydir = MYDIR; // set from the CONSTANT
$dir = opendir($mydir);
$DBSite = $_POST['DBSite'];
$getfilename = mysql_query("select distinct filename from search_test where site='" . $DBSite . "'") or die(mysql_error());
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
$result .= '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>';
}
if ($result) {
$result = "<table><tbody><tr><td>SITE Results.</td></tr> $result</table>";
}
return $result;
}
?>