hi all

i have a drop down list to lets user to select one option
then, i want to post this option to another page and display information from my database based on this option

how can i do this?

hint: the values of the list is stored in my database

Dani AI

Generated

A simple form/post is the right idea — showed the basic pattern — but for a robust solution when option values come from a database (as pointed out), follow three rules: populate the select with DB data, validate the posted value, and query the DB with parameterized statements. The short examples below illustrate a secure, practical workflow that avoids the old mysql_* functions and prevents SQL injection and XSS.

Populate the select from the database (use the record id as the option value, escape the label):

<?php
// $pdo = new PDO(...);
$stmt = $pdo->query('SELECT id, name FROM my_table ORDER BY name');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $id = (int)$row['id'];
    $label = htmlspecialchars($row['name'], ENT_QUOTES);
    echo "<option value=\"$id\">$label</option>\n";
}
?>

On the page that receives the form, validate the incoming value and use a prepared statement to fetch matching rows:

<?php
$selected = filter_input(INPUT_POST, 'item', FILTER_VALIDATE_INT);
if ($selected === false || $selected === null) {
    // handle missing or invalid selection
    exit;
}
$stmt = $pdo->prepare('SELECT * FROM items WHERE category_id = :id');
$stmt->execute([':id' => $selected]);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
    echo htmlspecialchars($row['title'], ENT_QUOTES), "<br>\n";
}
?>

Troubleshooting and notes: if an <option> lacks a value attribute, browsers submit its text as the value. Use GET when the result should be bookmarkable. For multiple selections use name="items[]" and validate an array of inputs. Always escape output with htmlspecialchars, validate inputs (whitelists or appropriate filters), and add a CSRF token for production forms. This approach keeps the flow from DB -> select -> POST -> safe query, and addresses the core of ’s question while improving security and reliability over the older examples.

Recommended Answers

All 4 Replies

anyone can help???????

<form action="yourpostedtopage.php" method="post">
<select name="yourselectbox" id="yourselectbox">
			<option value="something1">Option 1</option>
                        <option>Option 2</option>
</select>
<input name="submitbutton" type="submit" value="submit" />
</form>

yourpostedtopage.php

...
$selectedoption = $_POST['yourselectbox'];
...
echo $selectedoption;
mysql_query(".... WHERE column = '$selectedoption' ... ")
...

something like that?

thanks for helping me in my work

Thank alot for this post it is very helpfull my project

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.