简体   繁体   English

php while循环中的javascript onchange

[英]javascript onchange in php while loop

I have a form where radio types are generated inside a PHP while loop. 我有一种形式,其中在PHP while循环内生成无线电类型。 Depending on which radio is selected, java script queries MySQL to get the correct value and return it to a text box. 根据选择的无线电,java脚本查询MySQL以获取正确的值并将其返回到文本框。 It works except for onload. 除了onload以外,它都有效。 OnLoad returns the incorrect value, whatever was first in the while loop iteration. 无论在while循环迭代中是什么,OnLoad返回的值都不正确。 Onchange will return the right value. Onchange将返回正确的值。

How can I get onload to return the right value? 如何获得onload以返回正确的值? I am using "checked" in my radio throughout the while iteration so that one circle is always checked. 在整个while迭代过程中,我一直在收音机中使用“ checked”,以便始终选中一个圆圈。 It doesn't matter to me which radio is checked by default. 对我而言,默认情况下检查哪个收音机都没有关系。 Using checked in the while iteration always makes the last iteration checked which is OK, but it is displaying the value for the first. 在while迭代中使用checked总是会检查最后一次迭代,这是可以的,但它会显示第一个迭代的值。

here is my code. 这是我的代码。

<script type="text/javascript">
function getNextcheck(str)
{
if (str=="")
  {
  document.getElementById("nextcheck").innerHTML="";
return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("nextcheck").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getnextcheck.php?id="+str,true);
xmlhttp.send();
}
window.onload = function() {
document.getElementsByName('ID')[0].onchange();
}


</script>

And the file getnextcheck.php it pulls 和它拉的文件getnextcheck.php

<?php
include $_SERVER['DOCUMENT_ROOT']."/connect.php";


$result = mysql_query("SELECT * FROM bankaccount WHERE ID = '$_GET[id]'");
$row = mysql_fetch_array($result);

$nextcheck = $row['next_check'];

if (empty($nextcheck)) { $nextcheck = 0; }

echo "<tr><td>Check Number <input type=\"text\" name=\"checkno\" value=\"".$nextcheck."\" /></td></tr>";

And the radio inside the form. 和收音机里面的表格有关。

while($row2 = mysql_fetch_array($result2)) {
$id = $row2['ID'];

echo "<tr><td><input type=\"radio\" name=\"ID\" value=\"".$row2['ID']."\" onchange=\"getNextcheck(this.value)\" checked></td><td>".$row2['name']."</td><td>".$row2['acctnum']."</td>";
}

Thank you for your help. 谢谢您的帮助。

make only first radio check 只做第一次无线电检查

<?
$i=0;
while($row2 = mysql_fetch_array($result2)) {
$id = $row2['ID'];
$checked = $i?'':'checked'; 
echo "<tr><td><input type=\"radio\" name=\"ID\" value=\"".$row2['ID']."\" onchange=\"getNextcheck(this.value)\" {$checked}></td><td>".$row2['name']."</td><td>".$row2['acctnum']."</td>";
$i=1;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM