此代码允许某人从下拉菜单中选择一个类,我将数字转换为字母。现在我想将所选值发送回服务器:
{{1}}
如何从下拉菜单中传递选定的值' class'到服务器?我可以传递代码,但不知道如何传递选定的类。
答案 0 :(得分:0)
要将所选类添加到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>
在上面的代码中,您需要更改&#34;某些内容&#34;要使用正确的URL参数来传递所选的&#34;字母表&#34;值。
如果表行中有另一个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;
}