Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only one select list works on PHP form 1

Status
Not open for further replies.

kristo5747

Programmer
Mar 16, 2011
41
US
**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:

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

Here is the source of what is created to a pastebin:
 
For a reason I can't figure out, whatever option I select from the second select list is never captured when I hit Submit.

Your conditional statement precludes the possibility of both searches happening at the same time.

The only way the search for the second dropdown can happen is if the first does not have a value, that is its empty. Which will never be the case because your dropdown is created with a value selected from the start.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Either give your first dropdown an empty value so your conditional works, or change your conditionals.

If you need to fire both searches when there drodpown values are not empty then just remove the else part of your second if so it runs both conditionals.

If you only need to have one run, then give your dropdowns an extra option with an empty value. Otherwise you are checking for something that's never going to happen.

Code:
<option value="">Choose and Option</option>
...



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Complete re-write here =>
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top