如何通过单击单个按钮从众多下拉列表中发送数据?

时间:2017-04-10 17:38:40

标签: php html mysql html-select

有一张存储学生信息的表格。

CREATE TABLE pupis (name TEXT, city TEXT, caretaker TEXT);

打开this web-site,会显示此表中的信息。 有一个“排序”按钮,按城市排序所有学生。 此外,通过单击此按钮,每个学生附近都会出现一个下拉列表。 此列表允许为学生选择看护人。

这样,我的问题是如何通过点击单个按钮从众多下拉列表中发送所有选定的看管人员?(更新表)

对不起,我是PHP新手,现在我不明白如何来到这里。我只能建议动态创建包含pupil id及其看管人的数组。

我已经为此问题创建了this web-site,此代码就是该网站的工作原理:

<?php
mb_internal_encoding("UTF-8");
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
    or die('' . mysql_error());
mysql_select_db($mysql_dbname) or die('');
mysql_set_charset('utf8');

$query = 'SELECT * FROM pupils';
$result = mysql_query($query) or die(mysql_error());

echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

mysql_free_result($result);
mysql_close($link);
?>

<form action="" method="post">
    <input type="submit" name="sort" value="Sort">
</form>

<?php

if (isset($_POST["sort"]))  {
    $link = mysql_connect($mysql_host, $mysql_user, $mysql_password) 
        or die(mysql_error());
    mysql_select_db($mysql_dbname) or die();
    mysql_set_charset('utf8');

    $query = 'SELECT * FROM pupils  WHERE city = "Moscow";' or die("died");
    $result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result) != 0) {
        echo "<h1>Moscow</h1>";
        echo "<table>\n";
        while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
            echo "\t<tr>\n";
            foreach ($line as $col_value) {
                echo "\t\t<td>$col_value</td>\t\t";
            }

            echo "<td><form action=\"\" method=\"post\">";
                echo "<select name=\"choose_caretaker\" required>";
                    echo "<option value=\"0\">Choose a caretaker</option>";
                    echo "<option value=\"Caretaker1\">Caretaker1</option>";
                    echo "<option value=\"Caretaker2\">Caretaker2</option>";
                    echo "<option value=\"Caretaker3\">Caretaker3</option>";
                echo "</select></td>";
            echo "</form>";

            echo "</tr>\n";
        }
        echo "</table>\n";
    }

    $query = 'SELECT * FROM pupils  WHERE city = "London";' or die("died");
    $result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result) != 0) {
        echo "<h1>London</h1>";
        echo "<table>\n";
        while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
            echo "\t<tr>\n";
            foreach ($line as $col_value) {
                echo "\t\t<td>$col_value</td>\t\t";
            }

            echo "<td><form action=\"\" method=\"post\">";
                echo "<select name=\"choose_caretaker\" required>";
                    echo "<option value=\"0\">Choose a caretaker</option>";
                    echo "<option value=\"Caretaker1\">Caretaker1</option>";
                    echo "<option value=\"Caretaker2\">Caretaker2</option>";
                    echo "<option value=\"Caretaker3\">Caretaker3</option>";
                echo "</select></td>";
            echo "</form>";

            echo "</tr>\n";
        }
        echo "</table>\n";
    }

    mysql_free_result($result);
    mysql_close($link);
}?>

<form action="" method="post">
    <input type="submit" name="send_to_caretakers" value="Send">
</form>

<?if (isset($_POST["send_to_caretakers"]))  {  

    $link = mysql_connect($mysql_host, $mysql_user, $mysql_password) 
        or die(' ' . mysql_error());
    mysql_select_db($mysql_dbname) or die('');
    mysql_set_charset('utf8');

    echo "Sent to the caretakers";
    $temp = $_REQUEST['choose_caretaker'];
    echo $temp;
    // Have no idea how it can designed

    mysql_close($link); // Закрываем соединение
}
?> 

1 个答案:

答案 0 :(得分:0)

  1. 使用数组作为“选择名称”,例如choose_caretaker [pupil_id]
  2. 在选择框中添加“倍数”以选择多个
  3. 在php中渲染您的结果。你会看到你的看护人在阵列格式的学生下面。

      echo "<td><form action=\"\" method=\"post\">";
            echo "<select name=\"choose_caretaker[$link['id']]\" multiple required>";
                echo "<option value=\"0\">Choose a caretaker</option>";
                echo "<option value=\"Caretaker1\">Caretaker1</option>";
                echo "<option value=\"Caretaker2\">Caretaker2</option>";
                echo "<option value=\"Caretaker3\">Caretaker3</option>";
            echo "</select></td>";
        echo "</form>";