简体   繁体   中英

How do I make my drop down select menu persistently show the same data after a query is done using the menu?

So I am having an issue with a drop down selection box. What I am doing is having someone log into a database, and then the database shows all the tables available in the selection box. The user can then select the table they wish to see, hit select and bam! There's the table information.

I am, however having an issue getting the data in the selection box to persist after they hit select. For some reason, it just makes it empty. I'm using session variables, and maybe that effects it? I'm just now beginning to learn how that works too. Take a look at let me know what you think:

<?php
session_start();

if(!isset($_SESSION['session_level'])):
    $_SESSION['session_level'] = 0; ?>
<? endif ?>
<?php
if(isset($_POST['host'])):
    $_SESSION['host'] = $_POST['host'];
    $_SESSION['dbname'] = $_POST['dbname'];
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['pw'] = $_POST['pw'];
?>
<?php endif ?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8' />
        <title>Login Test</title>
    </head>

    <body>

        <?  
        if (isset($_POST['return']))
        {
            $_SESSION['session_level'] = 0;
        }
        else if (isset($_POST['submit']))
        {   
            try
                {
                    $db = new PDO("mysql:host=".$_POST['host'].";dbname=".$_POST['dbname'], $_POST['username'], $_POST['pw']);  

                }
            catch(Exception $error)
                {
                $_SESSION['session_level'] = 0;?>
                    <a href='<?= $_SERVER['PHP_SELF'] ?>'>Click here to return.</a> 
                    <? echo "\n"; ?>
                <?die("Connection to user database failed: " . $error->getMessage());

                }
            try
                {
                $db->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
                $query = "SHOW TABLES";
                $results = $db->query($query)->fetchAll();
                $_SESSION['session_level'] = 1;
                }
            catch(Exception $error)
                {
                    echo "Problem with query!";
                    $_SESSION['session_level'] = 0;?>
                <a href='<?= $_SERVER['PHP_SELF'] ?>'>Click here to return.</a> 
            <?  }
        }
        ?>
        <?php if($_SESSION['session_level'] == 0){?>
        <h1>Database Practice</h1>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='initialentry'>
            <table border='0' style='text-align: center'>
                <tr>
                    <td style='text-align: right;'>Enter host name:</td>
                    <td style='text-align: left;'>
                    <input type='text' name='host' value='localhost'>
                    </td>
                </tr>
                <tr>
                    <td style='text-align: right;'>Enter database name:</td>
                    <td style='text-align: left;'>
                    <input type='text' name='dbname' value='zxyx999'>
                    </td>
                </tr>
                <tr>
                    <td style='text-align: right;'>Enter user name:</td>
                    <td style='text-align: left;'>
                    <input type='text' name='username' value='zxyx999'>
                    </td>
                </tr>
                <tr>
                    <td style='text-align: right;'>Enter password:</td>
                    <td style='text-align: left;'>
                    <input type='password' name='pw' width='15' value='12345'>
                    </td>
                </tr>
                <tr>
                    <td style='text-align: right;'><input type="reset" name="reset" value="Reset"></td>
                    <td style='text-align: left;'><input type="submit" name="submit" value="Submit"></td>
                </tr>
            </table>
        </form>
<?php }


else if ($_SESSION['session_level'] == 1)
{
    ?>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
            <select name='select'>
                <? foreach($results as $row)
                    echo "<option value=" . $row[0] .">" .$row[0]. "</option>"; ?> 
            </select>
            <input type="submit" name="selected" value="Select">
            <input type="submit" name="return" value="Return to Main Screen">           
        </form>
    <?php   


    if(isset($_POST['selected']))
    {
        try
        {
            $db = new PDO("mysql:host=".$_SESSION['host'].";dbname=".$_SESSION['dbname'], $_SESSION['username'], $_SESSION['pw']);
        }
        catch(Exception $error)
        {
            die("Connection to user database failed: " . $error->getMessage());
        }

        try
        {
            $query = $db->prepare("SELECT * FROM " . $_POST['select']);
            $query->execute();
            $header = true;
        }
        catch(Exception $error)
        {
            echo "Query failed.";
        }
        echo "</br>";

        ?>

        <?php

        echo "<table border='1'>";

        while ($row = $query->fetch(PDO::FETCH_ASSOC))
        {
            echo "<tr>";
            if($header == 'true')
            {
                foreach($row as $index => $fieldValue)
                {
                    echo "<td>";
                    echo $index;
                    echo"</td>";
                }
            echo "</tr>";
            $header = 'false';  
            }

            echo "<tr>";
            foreach($row as $index => $fieldValue)
            {
                echo "<td>";
                echo $fieldValue;
                echo "</td>";
            }
            echo "</tr>";
        }

        echo "</table>";

    }
}

        ?>

</body>
</html>

When you repopulate the select list, if the option selected matches the option being populated, then you can make that bit of code <option selected value=" and that option will now be the default selected item at the moment.

I realize the below code isn't PHP, my point is that it should be a simple If statement, to include the selected tag into the option.

 /* Adding the "selected" tag to an option, makes it the default. */ 
 <select> <option value="" style="display:none;">Select a Value</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <br /><br /> <select> <option value="" style="display:none;">Select a Value</option> <option value="1">1</option> <option value="2">2</option> <option value="3" selected>3</option> <option value="4">4</option> </select> 

If I well understand your problem then my advice 'll be to change your form and do it this way :

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
        <input type="hidden" name="selected" value="true">
        <select name='select'>
            <? foreach($results as $row)
                echo "<option value=" . $row[0] .">" .$row[0]. "</option>"; ?> 
        </select>
        <input type="submit" value="Select">
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
        <input type="hidden" name="return" value="true">
        <input type="submit" value="Return to Main Screen">
</form>

It means you must create 2 forms instead of 1 with because the name value on a submit button is not intrepreted the same way on all browser. So by removing and replacing it by an input type hidden it 'll assure the field 'll exist after submitting forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM