1

我有一个简单的 Javascript 验证代码,如果我将它发送到某个 PHP 地址,它可以正常工作,而如果我将它发送到我创建的 PHP 页面,它就不起作用。

该代码目前非常基本,并使用正则表达式验证某些字段。

基本上,如果我使用给定的现成 PHP 文件(我无法访问)尝试它,它会很好地向我显示错误对话框,直到所有字段都正确为止。

同时,在我制作的 PHP 页面中,它捕获了错误,显示对话框,但在 OK 警告框上单击,它将字段提交到我的 PHP 文件。

有人可以告诉我发生了什么吗?

谢谢你。

这是Javascript:

<script type="text/javascript">
  /* <![CDATA[ */
  // this function calls all the other functions that validate each field of the submitting form
  function validate() {
    var validated = false;
    validated = validateRId() && validateEId();
    return validated;
  }
  // this validates the RID: checking that the field is filled with a number between 1    //and 99999
  function validateRId() {
    var RIdElement =  document.getElementById("RID");
    patternRId = /^([1-9][0-9]{0,4})$/;
    if (patternRId.test(RIdElement.value)) {
      return true;
    } else {
      alert ("Please Enter your RID (a number range 1 to 99999))");
      RIdElement.focus();
      return false
    }
  }
  // this validates the EventID: checking that the field is filled with a number between 1 and 99999
  function validateEId() {
    var EIdElement =  document.getElementById("EID");
    patternEId = /^([1-9][0-9]{0,4})$/;
    if (patternEId.test(EIdElement.value)) {
      return true;
    } else {
      alert ("Please Enter the EID (a number range 1 to 99999))");
      EIdElement.focus();
      return false
    }
  }
  /* ]]> */
</script>
<p/>
  <form action="http://adress.php" method="post" name="submitrunnertime"  onsubmit = "return validate()">
    <table>
      <tr><td>Runner ID*</td>
        <td><input type="text" name="RID" id="RID" size="5" maxlength="5"/></td>
      </tr>
      <tr><td>Event ID*</td>
        <td><input type="text" name="EID" id="EID" size="5" maxlength="5"/></td>
      </tr>
    </table>
    <input type="submit" name="submit" value="submit"/>
    <hr/>
  </form>
</body>
</html>

这是PHP:

<body>
<?php
  // access details as variables
  $username = "xxxx";
  $password = "tttt";
  $hostname = "rrrrr";
  //connection
  $connection = mysql_connect($hostname, $username, $password) or die ('connection problem:' . mysql_error());
  echo $connection . "CONNECTION SUCCEDED<br><br><br>";
  //select a database
  $mydb = mysql_select_db("xxxx", $connection)or die ('db problem:' . mysql_error());
  echo $mydb . " DB SELECTED <br><br><br>";
?>
</body>
</html>
4

1 回答 1

1

您在 javascript 函数中有错误

尝试这个

function validate() 
{
    var validated = false;
    validated = validateRId() && validateEId() ;    
    return validated;
}
于 2013-05-18T14:10:00.880 回答