已编辑:我已将代码更改为如下并出现“NaN”错误!有人能给我一个线索,知道这是什么,并提示解决?感谢。
有人可以告诉我这段代码有什么问题吗?
以下是代码:
<?php
$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$SQL="SELECT sbstart, sbend FROM newchk";
$run=mysql_query($SQL,$con) or die ("SQL Error");
$nor=mysql_num_rows($run);
?>
<!DOCTYPE html>
<html>
<head><title></title>
<script type="text/javascript">
function ValidateData() {
var TextIn = document.getElementById('txtN');
if(TextIn.value == ""){
alert ("Text field is empty");
return false;
}else{
var TextIn = document.getElementById('options');
alert (<?php $i ?> + TextIn.value);
}
}
</script>
</head>
<body>
<form onsubmit="return ValidateData()" method="POST">
<select STYLE='width:90px'><?php
while ($rec = mysql_fetch_array($run))
{
for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
{
echo "<option id='options' value=''.$i.''>$i<br></option>";
}
}
?>
</select>
<input type="text" name="txtN">
<input type="submit" name="subN" value="Save">
</form>
</body>
</html>
答案 0 :(得分:0)
您应该在onsubmit
中传递表单详细信息,您的选择没有名称,您无法命名选项,您需要为选择框命名:
<form onsubmit="return ValidateData(this)" method="POST">
<select STYLE='width:90px' name='options'><?php
while ($rec = mysql_fetch_array($run))
{
for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
{
echo "<option value='$i'>$i<br></option>";
}
}
?>
</select>
<input type="text" name="txtN">
<input type="submit" name="subN" value="Save">
</form>
和你的职能:
<script type="text/javascript">
function ValidateData(form) {
if(form.txtN.value == ""){
alert ("Text field is empty");
return false;
} else {
alert (form.options[form.options.selectedIndex].value + form.txtN.value);
}
}
</script>
答案 1 :(得分:0)
试试这个:
alert (<?php echo $i; ?> + TextIn.value);
您目前编写代码的方式将输出:
alert ( + TextIn.value);
......这是无效的JS。