开发者

<select><option> and action associated with them

开发者 https://www.devze.com 2023-03-10 22:06 出处:网络
I am writing a reservation system. On the main page I would give a choice of category, viewed the equipment available for booking.

I am writing a reservation system. On the main page I would give a choice of category, viewed the equipment available for booking. For example I have code like this:

<select>
        <option value = "a">A</option>
        <option value = "b">B</option>
        <option value = "c">C</option>
        <option value = "d">D</option>
        <option value = "e">E</option>
</select>

I wish that each choice was associated with a separate query to the database, 开发者_StackOverflowand that was the result of a query dynamically displayed on the screen.

It would be great if you could show me some sample code.

Regards


$query = mysql_query("SELECT * FROM choices");
while($row=mysql_fetch_assoc($query)) {
  echo '<option value="'.$row['value'].'">'.$row['value'].'</option>';
}

If you need separate query for each choice the code doesns't change much:

$query = mysql_query("(SELECT * FROM choices) UNION (SELECT * FROM choices1) [etc]");
while($row=mysql_fetch_assoc($query)) {
  echo '<option value="'.$row['value'].'">'.$row['value'].'</option>';
}


There are two parts to your question; 1 - Detecting which query to run and 2 - Displaying the results dynamically.

Part 1: Detecting which query to run:

Given hard-coded choices and no parameters for the query, using your above code, you can determine which query to run using the following:

For the HTML part, as part of a form, create the select as you did above (but with a name)

<select name="querySelect">
    <option value="a">A</option>
    <option value="b">B</option>
</select>

And in the PHP:

$querySelect = $_GET['querySelect'];
switch($querySelect)
{
    case 'a':
        $sql = "SELECT * FROM TableA";
        break;
    case 'b':
        $sql = "SELECT * FROM TableB";
        break;
}

$results = mysql_query($sql);

Part 2: Displaying the results dynamically

With the $results, what you do with the data very much depends on what you want to achieve. At a very basic level, you can do the following to dynamically display a table of the results:

if(mysql_num_rows($results) > 0)
{
    $header = false;
    print "<table>"

    while($row = mysql_fetch_assoc($results))
    {
        if(!$header)
        {
            $headings = array_keys($row);
            print "<tr>";
            for($i=0;$i<count($headings);$i++)
            {
                print "<th>".htmlspecialchars($headings[$i])."</th>";
            }
            print "</tr>";

            $header = true;
        }

        print "<tr>";

        foreach($row as $value)
        {
            print "<td>".htmlspecialchars($value)."</td>";
        }

        print "</tr>";
    }

    print "</table>"
}
else print "<h1>No Results Found!</h1>";

mysql_free_result($results);

There is still alot not covered in my answer because I can't say what level of detail is required. You will also need to cover things like your connection to MySQL, error handling, formatting of the table...

Update Hmmm, very interested to know why this has been downvoted. If someone can please explain in comments where I have misinterpreted the question or misguided the user, I would appreciate it.


If you use jQuery the code might look like

<select id="select_id">
    <option value = "a">A</option>
    <option value = "b">B</option>
    <option value = "c">C</option>
    <option value = "d">D</option>
    <option value = "e">E</option>
</select>
<script type="text/javascript">
    $('#select_id').change(function(e) {
        // this code send selected value to the server
        $.post('url', {selected_value: this.value}, function(response) {
            // handle the server's response
        });
    });
</script>

On server side take the value from $_POST and make a query. And remember - do not trust to data from client-side. You never know who is over there. Always check incoming data and DO NOT use such constructions

$sql = "SELECT * FROM table_name WHERE name = '{$_POST['selected_value']}'";

because there might be any string including those can drop all databases, clear data and so forth.

0

精彩评论

暂无评论...
验证码 换一张
取 消