<?php
$conn=get_dbconnect();
$sql="select name from states";
$rs = mysqli_query($conn, $sql);//odbc_exec($conn,$sql);
if(mysqli_num_rows($rs) > 0) {
while($row = mysqli_fetch_assoc($rs)) {
echo "<input type='checkbox' name='states' value='".$row['name']."'; /> ".$row['name'];
}
}
mysqli_close($conn);
?>
<input type="submit" onclick="check('states')"> ?>
这是从数据库中获取状态值的代码。如何使用 JavaScript 获取数据库内每个状态的自动递增 id 的值。获得 id 后,我想将该值添加到另一个 SQL 表中。
另一种选择是在某些条件下使用 for 循环:
var allInputs = document.getElementsByTagName("input"), arr = [];
for (var i = 0, max = allInputs.length; i < max; i++){
if (allInputs[i].type === 'checkbox' && allInputs[i].checked){
arr.push(allInputs[i].value); // <----will push the checked one's values only
}
}
console.log(arr);
使用这个。
<input type='checkbox' name='states[]' value='".$row['id']."' id="st"/> ".$row['name'];
并像这样. 希望它会有所帮助.
if ($("#st").prop('checked'))
{
//blah blah
alert($("#st").val());
}
可以尝试将所有值放在数组中,然后将其发送到 SQL 数据库。
<?php
$conn=get_dbconnect();
$sql="select name from states";
$rs = mysqli_query($conn, $sql);//odbc_exec($conn,$sql);
$stateIds = array();
$cBoxId = 0;
if(mysqli_num_rows($rs) > 0) {
while($row = mysqli_fetch_assoc($rs)) {
$cBoxId = $cBoxId + 1;
echo "<input type='checkbox' id='".$cBoxId."' name='states' value='".$row['name']."'; /> ".$row['name'];
$stateIds[] = $cBoxId;
}
}
mysqli_close($conn);
?>
<input type="submit" onclick="check('states')"> ?>