こんにちは、javascript とその API が苦手です。これは、ラジオ ボタンを使用してデータベースから特定の行フィールドを 1 つ取得したいテーブルです。つまり、データベースからのすべての情報が表示され、そのうちの 1 つをチェックすると、たとえば、この行の領収書の値を表示しますが、値を取得できません。最初の警告ウィンドウでは、「選択された値は:オン」と表示され、2番目の値は「未定義」と表示されます
これは私のコードです:
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'];
}
?>