0

I made some PHP code to generate this page. I successfully get all the items from a column into a HTML dropdown list (it's a dynamic list). I want to write some code so that when user selects an item from the list and hit submit, it will take user to a new page contains corresponding information on it. I have no idea what kind of code would be included in. Please help. Thanks!

For instance, if user select 50A-1, it will populate a table has all the items located at 50A-1.

Two pieces of code I wrote, first is the page gives you the dropdown list and the submit button. The second is the result page, but it only shows the whole inventory so far, it doesn't have a way to connect to the dropdown list option.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Inventory</title>
    </head>
    <body>
        <div>
            <a>SQL Connection test</a>
            <form action="connect.php" method="POST">
                <div class="center">
                    <input type="submit" value="Connect to MySQL" />
                </div>
            </form>
        </div>
        <div>
            <section>
                <article>
                    <p>
                        <select name="dropdown">
                            <?php query() ?>
                        </select>
                            <?php close() ?>
                    </p>
                </article>
            </section>
            <div>
                <input type="submit" value="Submit" />
            </div>
        </div>
    </body>
</html>

Second page

<?php
    include_once 'db.inc.php';
    // connect
    function connect() {
        // Connect to the MySQL server
        mysql_connect(DB_HOST,DB_USER,DB_PASS) or die ('Could not connect to server!' . mysql_error());
        mysql_select_db(DB_NAME);
    }
    // close
    function close() {
        mysql_close();
    }
    // query
    function query() {
        $myData = mysql_query("SELECT DISTINCT * FROM sheet0_100 GROUP BY location");
        while($record = mysql_fetch_array($myData)) {
            echo '<option value="' . $record['location'] . '">' . $record['location'] . '</option>';
        }
    }
?>
10
  • What did you try ? Try to write some code, then come back and ask once you have a specific question. Commented Oct 1, 2014 at 18:20
  • @LorenzMeyer, I know how to write the code for the page contains the result after submit. But I have no idea how to write any code to make the dropdown list to have something to do with SQL.. Commented Oct 1, 2014 at 18:21
  • You want to trace the change event of dropdown using javascript/jquery? Commented Oct 1, 2014 at 18:25
  • 1
    Avoid using mysql. It's depreciated in the latest version. You beter switch to PDO. Commented Oct 1, 2014 at 18:30
  • 1
    Even if you fix your code and get it running you should avoid mysql commands and move to PDO. Commented Oct 1, 2014 at 18:47

2 Answers 2

1

That's the purpose of HTML forms :)

You need to create a form to encapsulate that select:

<form action="process.php" method="get">
    <select name="inventory_id">
        <!-- Here all options -->
    </select>
    <button type="submit">See items</button>
</form>

Then in process.php you need to get the selected element and query the database, for example (I assume that you're using PDO):

<?php
    $inventory_id = $_GET['inventory_id'] // The name attribute of the select
    // Then you prepare the query
    $query = "SELECT * FROM sheet0_100 WHERE id = :inventory_id";
    // Execute the query and show the data...
Sign up to request clarification or add additional context in comments.

7 Comments

Here is the way I wrote those options. <select name="dropdown"> <?php query() ?> </select>
would the way I get options work with your example code here?
@ShuruiLiu Yep, it would ;)
After going over some tutorials about PDO online, I don't quite understand the benefit of using it or how do i implement PDO into my code...Could you give me a hint maybe?
I don't see how it would be easier to use PDO than PHP + Ajax.
|
0

use Sessions

example:

on your first page

session_start();
$_SESSION['your-dropdown-list-value'] = 'Root';

on your new page

//error_reporting(E_ALL);
session_start();
if(isset($_SESSION['your-dropdown-list-value'])) {
  echo "Your dropdown selection " . $_SESSION['your-dropdown-list-value'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.