Forum Moderators: coopster

Message Too Old, No Replies

Only one select list works on PHP form

         

kristo5747

10:25 pm on Aug 24, 2011 (gmt 0)

10+ Year Member



**Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.**

I have a basic form that's meant to search flat files on our server. The "search engine" I created as two select lists: one for the file names and one for the customer site files come from.

For a reason I can't figure out, 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.

What am I missing? I am sure it's starting right at me.... Any hints welcome. Thank you.

Here's my code:

<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>";

}
?>

kristo5747

10:41 pm on Aug 24, 2011 (gmt 0)

10+ Year Member



Added the source of what is created to a pastebin: [pastebin.com...]

rocknbil

4:30 pm on Aug 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.


It's a combination of two things. The first is here:

<select name="DBFilename" id="DBFilename" size="0">
....
while ($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option> <?php }

So DBFilename is never empty. Then you test against that like so:

if (!empty($_POST['DBFilename'])) {doFileSearch();}
elseif (!empty($_POST['DBSite'])) {doSite();}

So it's always captured by the first if condition. There are two ways to fix it - the second we'll ignore, and that's to change the if/else if logic. The first is easiest and a Very Good Way to generate selects: always have an empty (or zero if it's numeric) value as the first value in a select:

<select name="DBFilename" id="DBFilename" size="0">
<option value="">Select Filename</option>

<select name="DBSite" id="DBSite">
<option value="">Select Site</option>

Now you'll get the site ones in the else if, but what happens if neither is selected? (Answer, it will ignore that section and just output the form, but let me move to my point . . .)

You should always include good error trapping and it's also very easy, but the inline method of your script makes it a little more difficult. :-) That is, it executes top to bottom, and you need the calls to doSite() AND doFile Search() right where they are. So the most **minimal** change would be to add at the top of the script, before the first form,

<body><h1>SEARCH TOOL - PROTOTYPE</h1>
<?php
if ((isset($_POST['submit']) and empty($_POST['DBFilename']) and empty($_POST['DBSite'])) {
echo "<p>Please select either a file search or a site search.</p>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

(A side note, google for vulerabilities PHP_SELF.)

But that's hard coded, a single value, and there may be other needs for error output at that location in your program. A much better way would be to return the values from your functions so you can put them where you want them. Also it's not necessary to open a mysql connection 3 times. Play around with this and see if you can get it working (untested code: )


<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;
}
?>


The mysql connection should remain within the functions and this may need debugging . . but should work. And it's a little more expandable. :-)

kristo5747

7:25 pm on Aug 25, 2011 (gmt 0)

10+ Year Member



You are an ace and a scholar.You explained in simple terms what I was overlooking:

a) Neither one of my SELECT lists are ever empty.
b) I never check for return the values from my functions.

I ran your code on my dev environment (with a few tweaks): it works awesome.

Kudos for taking the time. Much appreciated.