嗨,我不擅长 javascript 及其 API,这是一张表,我想用单选按钮从数据库中获取一个特定的行字段,我的意思是它显示了数据库中的所有信息,当我检查其中一个时,我想要它显示例如该行的收据值,但我无法获得该值。在第一次的警报窗口中,它显示“选定的值是:on”和第二个“未定义”
这是我的代码:
if ($term == "")
echo " .enter a usernumber";
else {
$query = mysql_query("select * from test
where username like '{$term}%'", $connection);
$string = '';
echo "<br>";
echo "<center>";
echo "<div>";
echo"<form onclick='onSubmit();'>";
echo "<div align='center' width = 900>";
echo "<table class='styled-table' cellspacing='0' width='900' border='1'>";
echo "<tr>";
echo "<th width='10' scope='col'>Check</th>";
echo "<th width='10' scope='col'>Username</th>";
echo "<th width='10' scope='col'>Password</th>";
echo "<th width='10' scope='col'>Name</th>";
echo "<th width='10' scope='col'>Last Name</th>";
echo "<th width='10' scope='col'>Midterm</th>";
echo "<th width='10' scope='col'>Class Mark</th>";
echo "<th width='10' scope='col'>Final</th>";
echo "<th width='10' scope='col'>State</th>";
echo "<th width='10' scope='col'>Level</th>";
echo "<th width='10' scope='col'>Teacher</th>";
echo "<th width='10' scope='col'>Class Num</th>";
echo "<th width='10' scope='col'>Receipt</th>";
echo "<th width='10' scope='col'>Date</th>";
echo "</tr>";
if (mysql_num_rows($query)) {
while ($row = mysql_fetch_assoc($query)) {
echo "<tr>";
echo "<td align='center'><input class='styled-input' type='checkbox' name='check' id='check' value= " . $row['check'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='user' id='user' value= " . $row['username'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='pass' id='pass' value= " . $row['password'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='name' id='name' value= " . $row['name'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='lastname' id='lastname' value= " . $row['lastname'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='midmark' id='midmark' value= " . $row['midmark'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='classmark' id='classmark' value= " . $row['classmark'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='finalmark' id='finalmark' value= " . $row['finalmark'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='state' id='state' value= " . $row['state'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='level' id='level' value= " . $row['level'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='teacher' id='teacher' value= " . $row['teacher'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='classnum' id='classnum' value= " . $row['classnum'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='receipt' id='receipt' value= " . $row['receipt'] ." ></td>";
echo "<td align='center'><input class='styled-input' type='text' name='date' id='date' value= " . $row['date'] ." ></td>";
echo "</tr>";
$_SESSION["suser"]=$row['username'] ;
if (($row['check']) == true)
{$_SESSION["sreceipt"] = $row['receipt'] ;}
}
echo "</table>";
echo "</div>";
echo"</form>";
echo "</div>";
echo "</center>";
} else {
$string = "nothing found !";
$_SESSION["suser"]='';
$_SESSION["sreceipt"]='';
}
echo $string;
}
?>
<script type="text/javascript">
function get_radio_value() {
var inputs = document.getElementsByName("check");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
var row = $(this).closest('tr'),
receiptValue = row.find('input[name=receipt]').val();
alert(receiptValue);
alert("selected input is: " +inputs[i].value);
}
}
}
function onSubmit() {
var id = get_radio_value();
}
</script>
为了投入$_post
,$_session['sreceipt']
我想做一些类似下面代码的事情:
<script type="text/javascript">
function get_radio_value() {
var cboxes = document.getElementsByName("check");
for (var i = 0; i < cboxes.length; i++) {
var cbox = cboxes[i];
if (cbox.checked) {
//Now you are switching to jQuery:
var receiptValue = $(cbox).closest('tr').find('input[name=receipt]').val()
var data = {};
data[cbox.value] = receiptValue;
$.post('editstudent.php', data,'text');
alert(receiptValue);
}
}
}
function submit_func() {
var id = get_radio_value();
}
</script>
<?php
if (isset($_POST['data'])) {
$_SESSION['sreceipt'] = $_POST['data'];
}
?>