生成下拉列表,将所选项目发送回服务器


Generate dropdown lists which will send the selectted item back to server

这段代码允许人们从下拉菜单中选择一个类,在下拉菜单中我将数字转换为字母。现在我想将选定的值发送回服务器:

<table id="example" class="display table" style="width: 100%; cellspacing: 0;">
    <thead>
        <tr>
            <th>Code</th>
            <th>Name</th>
             <th>Hours</th>
            <th>Class</th>
            <th>Add</th>
         </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Code</th>
            <th>Name</th>
             <th>Hours</th>
            <th>Class</th>
            <th>Add</th>
        </tr>
    </tfoot>
    <tbody>
<?php
$query = "SELECT * FROM class";
$result = mysqli_query($connection,$query) or  die ("Couldn’t execute query.");
while($row = mysqli_fetch_assoc($result))
{
    echo "<tr>                              
        <td>$row[code]</td>
        <td>$row[name]</td>
        <td>$row[hours]</td>";
    $query1 = "SELECT total FROM classtot where code='$row[code]'";
    $result1 = mysqli_query($connection,$query1);
    while ($row=mysqli_fetch_assoc($result1))
    {
        $a=$row['total'];
    }
    $alphabet = range('A','Z');
    $i = 1;
    echo "
    <td><select id='selectError' data-rel='chosen' name='class'>";
    while ($i<=$a)
    {
        $kls=$alphabet[$i-1];
        echo "<option value=$kls> $kls </option>";
        $i=$i+1;
    }
    echo "</select></td>
    <td>                                    
    <a class='btn btn-primary btn-addon m-b-sm btn-xs' href='home_member.php?page=add&id=$row[code]'>
    <i class='fa fa-plus'></i> Add</a>
    </td>
    </tr>";                                 
}
?>
    </tbody>
</table>  

如何将下拉菜单"class"中选定的值传递给服务器?我可以传递代码,但不知道如何传递选定的类。

要将所选类添加到URL,您需要用JavaScript构建URL,因为PHP事先不知道用户会选择什么。这是你的代码(只是通过SQL的部分。在评论中,我描述了我所做的几个改进(与你的问题无关):

<?php
// Make your query return ALL you need. Avoid second query:
$query = "SELECT     class.code, class.name, class.hours, classtot.total
          FROM       class
          INNER JOIN classtot ON classtot.code = class.code";
$result = mysqli_query($connection,$query) or die ("Couldn’t execute query.");
// Keep track of row number, for use in generating unique id property values 
$rowIndex = 0;
while($row = mysqli_fetch_assoc($result))
{
    $rowIndex++;
    echo "<tr>                              
             <td>$row[code]</td>
             <td>$row[name]</td>
             <td>$row[hours]</td>";
    // Just pick the total from the combined query:
    $a = $row['total'];
    $alphabet = range('A','Z');
    // Change id for select: You cannot assign the same id to several elements
    echo "
    <td><select id='select$rowIndex' data-rel='chosen' name='class' >";
    // Use a zero-based for loop instead of a while
    for ($i = 0; $i < $a; $i++)
    {
        // Reference $i now, not $i-1:
        $kls = $alphabet[$i];
        echo "<option value='$kls'> $kls </option>";
    }
    // Instead of hard-coding the URL, call a JS function that will make the url
    echo "</select></td>
    <td> 
    <a class='btn btn-primary btn-addon m-b-sm btn-xs' href='#'
       onclick='gotoClass($rowIndex, '"$row[code]'")'>
    <i class='fa fa-plus'></i> Add</a>
    </td>
    </tr>";
}
?>
    </tbody>
</table>
<script>
    // The JS function that builds the URL and triggers the navigation to it
    function gotoClass(rowIndex, classCode) {
        var sel = document.getElementById('select' + rowIndex);
        location.href = 'home_member.php?page=add&id=' + classCode 
                      + '&something=' + sel.value;
        return false; 
    }
</script>

在上面的代码中,您需要将"something"更改为要用于传递所选"alphabet"值的正确URL参数。

如果表行中有另一个select,如下所示:

<select id='select2$rowIndex' data-rel='chosen' name='code' >

然后将上面的javascript函数扩展如下:

    function gotoClass(rowIndex, classCode) {
        var sel = document.getElementById('select' + rowIndex);
        var sel2 = document.getElementById('select2' + rowIndex);
        location.href = 'home_member.php?page=add&id=' + classCode 
                      + '&something=' + sel.value
                      + '&othervalue=' + sel2.value;
        return false; 
    }