gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:30:38 25 4
gpt4 key购买 nike

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

<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>

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

最佳答案

要将所选类添加到 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;
}

关于php - 生成下拉列表,将所选项目发送回服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33983057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com