PHP中的JS ..警报不显示

时间:2012-09-09 14:50:49

标签: php javascript

已编辑:我已将代码更改为如下并出现“NaN”错误!有人能给我一个线索,知道这是什么,并提示解决?感谢。


有人可以告诉我这段代码有什么问题吗?

  1. 首先,我在PHP中调用了一个Javascript,这真的可能吗?
  2. 我拨打第二个提醒的方式有问题吗? (尽管是否有可能在php中使用j),这意味着显示文本输入和警报消息中选择的选项。
  3. 我是否需要在循环的最后一个大括号之后放置'行尾'(;)?
  4. 以下是代码:

    <?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> 
    

2 个答案:

答案 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。