kristo5747 Posted August 24, 2011 Share Posted August 24, 2011 **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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/ Share on other sites More sharing options...
kristo5747 Posted August 24, 2011 Author Share Posted August 24, 2011 Added the source of what is created to a pastebin: http://pastebin.com/raw.php?i=mjxkVEmW Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261605 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 Are you sure about this??? select filename from search_test where filename='".$DBFilename."' you're selecting filename where filename equals the filename you already have. Kind of redundant. Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261609 Share on other sites More sharing options...
kristo5747 Posted August 24, 2011 Author Share Posted August 24, 2011 Are you sure about this??? select filename from search_test where filename='".$DBFilename."' you're selecting filename where filename equals the filename you already have. Kind of redundant. I am sure there better ways to do that...all I want to do is provide a URL to the file located on the server for the file selected from the list. If you have better suggestion, I am all ears. Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261612 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 can you post the list of fields you have in table `search_test` please ? Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261614 Share on other sites More sharing options...
kristo5747 Posted August 24, 2011 Author Share Posted August 24, 2011 filename site script station result stbmodel rid testdate Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261619 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 I sort of re-wrote your code, created a function to handle mysql connection, and put functions nice and tidy at top of page. However, I cannot test this code because I don't have the database here. try this: <?php // functions function con(){ $c = mysql_connect("localhost", "user", "pass") or die('Could not connect: ' . mysql_error()); mysql_select_db("dev", $con) or die(mysql_error()); return $c; } function doSite($DBSite) { $mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot"; $dir = opendir($mydir); $con = con(); $getfilename = mysql_query("select distinct `filename` from `search_test` where `site`='$DBSite'",$con) or die(mysql_error()); $result = '<table><tr><td>Results.</td></tr>'; while ($row = mysql_fetch_array($getfilename)){ $result .= '<tr><td><a href="' . basename($mydir) . '/' . $row['filename'] . '" target="_blank">' . $row['filename'] . '</a></td></tr>'; } @mysql_close($con); $result .= '</table>'; return $result; } function doFileSearch($DBFilename) { $mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot"; $dir = opendir($mydir); $con = con(); $getfilename = mysql_query("select `filename` from `search_test` where `filename`='$DBFilename'",$con) or die(mysql_error()); $result = '<table><tr><td>Results.</td></tr>'; while ($row = mysql_fetch_array($getfilename)) { $result .= '<tr><td><a href="' . basename($mydir) . '/' . $row['filename'] . '" target="_blank">' . $row['filename'] . '</a></td></tr>'; } @mysql_close($con); $result .= '</table>'; } ?> <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="1"> <?php $con = con(); $result = mysql_query("select distinct `filename` from `search_test` order by `filename`",$con); while ($row = mysql_fetch_array($result)){ echo '<option value="'.$row['filename'].'">'.$row['filename'].'</option>'; } @mysql_close($con); ?> </select></fieldset> <fieldset> <legend>Site (one item)</legend><select name="DBSite" id="DBSite" size="1"> <?php $con = con(); $result = mysql_query("select distinct `site` from `search_test` order by `site`",$con); while ($row = mysql_fetch_array($result)){ echo '<option value="'.$row['site'].'">'.$row['site'].'</option>'; } @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><br /><br /> <?php if (isset($_POST['submit'])){ if (!empty($_POST['DBFilename'])){ echo doFileSearch($_POST['DBFilename']); } echo '<br /><br />'; if(!empty($_POST['DBSite'])){ echo doSite($_POST['DBSite']); } } ?> </body></html> turn on errors, 'cause there's bound to be a typo somewhere in there, as it is all untested Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261621 Share on other sites More sharing options...
kristo5747 Posted August 24, 2011 Author Share Posted August 24, 2011 I sort of re-wrote your code, created a function to handle mysql connection, and put functions nice and tidy at top of page. However, I cannot test this code because I don't have the database here. try this: <?php ............... I tried your code but to no avail. The first SELECT works fine. The second SELECT is never submitted. Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261625 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 can you do a dump of your table in .sql format so I can set it up here? Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261628 Share on other sites More sharing options...
kristo5747 Posted August 24, 2011 Author Share Posted August 24, 2011 Ignore my previous posts. I did not fix all typos. It's working now.Thank you!! It's working now but it seemingly introduced a new problem: I can't seem to do more than one search at a time. The result shows up again and again. I am using Firefox 6.0 on Ubuntu. Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1261630 Share on other sites More sharing options...
kristo5747 Posted August 25, 2011 Author Share Posted August 25, 2011 Complete re-write here => http://pastebin.com/raw.php?i=qi5F7X2e There are several problems with my form. a) Neither one of my SELECT lists are ever empty. b) I never check for return the values from my functions. This one is working. Thanks for taking the time. Quote Link to comment https://forums.phpfreaks.com/topic/245634-only-one-select-list-works-on-php-form/#findComment-1262009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.