我编写html和php代码 现在html代码按钮不能连接fuction事件(就是检测注册数据是不是空 用alert没有响应) 谢谢帮我看看是哪里的问题 还有就是我在php编写的检测用户名和密码 邮箱等格式 功能也不能实现 不知道问题出在哪 数据库都没有问题(连接文件lr.php) 代码如下:
html文件:问题处用红色标出了<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
         <title>Register</title>
            <style type="text/css">
            html{font-size:18px;}
            fieldset{width:960px; margin: 0 auto;}
            legend{font-weight:bold; font-size:15px;}
            label{float:left; width:300px; margin-left:10px;}
            left{margin-left:50px;}
            input{width:250px;}
            span{color: #666666;}
            </style>
               <script language="JavaScript">
                     <!--
                  function InputCheck(RegForm){
               if (RegForm.userName.value == "")
  .                        {
                      alert("not null !");
                   RegForm.userName.focus();
                   return (false); }
  .            if (RegForm.passWord.value == "")
                           {
  alert("need passWord!");
RegForm.passWord.focus();
                   return (false); }
               if (RegForm.repass.value != RegForm.passWord.value)
                           {
                   alert("not same!");
                   RegForm.repass.focus();
                   return (false); }
               if (RegForm.email.value == "")
                           {
                   alert("not null!");
                   RegForm.email.focus();
                   return (false);
                            }
  }
                     //-->
            </script>

    </head>
        <body>
           <div>
              <fieldset>
                 <legend>Login</legend>
                    <form action="regstinfo.php" method="post" ><p>
                       <label for="userName" class="label">UserName:</label>
                          <input id="userName" name="userName" type="text" class="input" /></p>
                             <p>
                                <span>(must be 3-15 long with letters,numbers or '_')</span></p>
                             <p>
                       <label for="passWord" class="label">passWord:</label>
                          <input id="passWord" name="passWord" type="passWord" class="input"></p><p>
                                 <span>(must be 6 units)</span></p>
                             <p>
                       <label for="repass" class="label">passWord again:</label>
                          <input id="repass" name="repass" type="passWord" class="input"></p>
                             <p>
                       <label for="firstName" class="label">FirstName:</label>
                          <input id="firstName" name="firstName" type="text" class="input"></p><p>
                             <span>must be full</span></p>
                             <p>
                       <label for="LastName" class="label">LastName:</label>
                          <input id="lastName" name="lastName" type="text" class="input"></p><p>
                             <span>must be full</span></p>
                             <p>
                       <label for="gender" class="label">Gender:</label>
                        <Form><input id="Male" name="gender" type="radio" value="Male" checked>Male
                             <input id="Female" name="gender" type="radio" value="Female">Female</p>
                        </Form><p>
                       <label for="type" class="label">type:</label>
                             <input id="Client" name="type" type="radio" value="Client" checked>Client
                             <input id="Farmer" name="type" type="radio" value="Farmer">Farmer</p>
                             <p>
                       <label for="phoneNumber" class="label">PhoneNumber:</label>
                          <input id="phoneNumber" name="phoneNumber" type="text" class="input"></p><p>
                             <span>must be full</span></p>
                             <p>
                       <label for="road" class="label">Road:</label>
                          <input id="road" name="road" type="text" class="input"></p><p>
                             <span>must be full</span></p>
                             <p>
                       <label for="zipcode" class="label">Zipcode:</label>
                          <input id="zipcode" name="zipcode" type="text" class="input"></p><p>
           <span>must be full</span></p>
                             <p>
                       <label for="insecCode" class="label">InsecCode:</label>
                          <input id="insecCode" name="insecCode" type="text" class="input"><p>
                             <span>must be full</span></p>
                             <p>
                       <label for="email" class="label">Email:</label>
                          <input id="email" name="email" type="text" class="input"></p><p>
                             <span>must be full</span></p>                  
                              <p>
  <td>Verification Code:</td><td><input type="text" name="captcha_code" size="10" maxlength="6" /></td><img id="captcha" src="checkNumber.php" alt="Reflash">
        <a href="#" onclick="document.getElementById('captcha').src = 'checkNumber.php?' + Math.random(); return false">Reload Image</a>
                          
     </p>
                             <p>
                          <input type="submit" name="submit" value="SUBMIT" class="left" onclick="InputCheck(RegForm);">
                          <input type="reset" name="reste" value="RESET" class="left">
                             </p>
            </form>
         </fieldset>
      </div>
   </body>
</html>这个是php:红色功能没实现:
<?php require_once('lr.php'); ?> 
<?php
if(!isset($_POST['submit'])){
    exit('Not allow!');
}
session_start();
$code=$_POST["passcode"];
echo $code;
echo "</br>";
$user_name = $_POST['userName'];
echo "username:$user_name";
echo "</br>";
$user_pass = $_POST['passWord'];
echo "password:$user_pass";
echo "</br>";
$firstName=$_POST['firstName'];
echo "firstName:$firstName";
echo "</br>";
$lastName=$_POST['lastName'];
echo "lastName:$lastName";
echo "</br>";
$gender=$_POST['gender'];
echo "gender:$gender";
echo "</br>";
$type=$_POST['type'];
echo "type:$type";
echo "</br>";
$phoneNumber=$_POST['phoneNumber'];
echo "phoneNumber:$phoneNumber";
echo "</br>";
$email = htmlspecialchars($_POST['email']);
echo "email:$email";
echo "</br>";
mysql_select_db($db_name, $tbl_name);
$sql="select * from Person WHERE userName='$user_name'";
echo $sql;
echo "</br>";
$res=mysql_query($sql)or die("failed search!");if(is_array(mysql_fetch_row($res)))
{
echo "this user is existe!";}
if($number   !=   $login_check_number   ||   empty($number))
{
        print( "checknumber is wrong! ");
        die();
}
if(!preg_match('/^[\w\x80-\xff]{3,15}$/', $user_name)){
    exit('Wrong:Not allow this modle。<a href="javascript:history.back(-1);">Go back</a>');
}
if(strlen($user_pass) < 3||strlen($user_pass) > 15 ){
    exit('E-mail address not valid.<a href="javascript:history.back(-1);">Go back/a>');
}if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
    die('Wrong:there is problem of your email。<a href="javascript:history.back(-1);">Go back</a>');
}
include('lr.php');
$check_query = mysql_query("select personId from MadeInMaFerme where userName='$user_name' limit 1");
if(mysql_fetch_array($check_query)){
    echo 'Wrong:userName',$user_name,' it is exist.<a href="javascript:history.back(-1);">Go back</a>';
    exit;
}
if (isset($_POST)){
$code=trim($_POST['captcha_code']);
include_once("checkNumber.php");
$img=new checkNumber();
$valid=$img->check($code);
if($valid==true)
{echo "checkNumber is right";
}else
{echo "checkNumber is wrong";}
}

$user_pass = MD5($user_pass);
$sql = "INSERT INTO Person(userName,passWord,firstName,lastName,gender,type,phoneNumber,email)VALUES('$user_name','$user_pass','$firstName','$lastName','$gender','$type','$phoneNumber','$email')";
echo $sql;
mysql_query($sql)or die ("Failed registration");if(mysql_query($sql,$con)){
    exit('Be Successful!Click here<a href="login.html">Enter</a>');
} else {
    echo "<script>alert(Sorry!you are failde:',mysql_error()</script>";<br />';
    echo 'click here <a href="javascript:history.back(-1);">Go back</a> Retry';
}
}
else{ echo "<script>alert('wrong with checknum');history.go(-1);</script>";}
}
?>

解决方案 »

  1.   

    要获取表单数据,最好用:
    var name=document.getElementById("name");
    alert(name.value);
      

  2.   

    要在表单提交时进行表单验证,在submit按钮上添加onclick事件是不可行的,因为submit按钮貌似不支持onclick事件,你可以将表单验证事件添加到form表单的上<form action="regstinfo.php" method="post"  onsubmit='return InputCheck(RegForm)'><p>
      

  3.   


    if (name.value=='')
    {
    alert('我擦!');
    }
      

  4.   

    if(name.value=="")
    {
        echo 'Please input the value';       
    }
      

  5.   

    1、你有
    if (RegForm.userName.value == "")
    . {
    alert("not null !");
    RegForm.userName.focus();
    return (false); }
    . if (RegForm.passWord.value == "")
    那些前导的“.”将产生错误,导致函数定义失败2、<input type="submit" name="submit" value="SUBMIT" class="left" onclick="InputCheck(RegForm);">
    中的 InputCheck(RegForm); 将在提交前的页面中执行,而不是提交后的页面
    你需要在表单的 onsubmit 事件中调用
      

  6.   

    if (RegForm.userName.value == "")
    是不是 RegForm 指表单的ID?<form action="regstinfo.php" method="post" ><p>
    是不是应该定义下ID了  
      

  7.   

    谢谢 我改正好了 但是遇到新问题 就是验证码 如何在html里面检验:
    可以找到键盘输入值captcha_code.value  如何对比 图片产生值啊 我的验证码代码是
    <?php
    $num="";
    for($i=0;$i<4;$i++){
    $num=rand(0,9);
    }
    session_start();
    $_session["Checknum"]=$num;
    Header("Content-type: image/png");
    srand((double)microtime() * 100000);
    $login_check_number   =   strval(rand( "1111 ", "9999 "));
    session_register("login_check_number");
    $_SESSION['login_check_number']=$login_check_number;$h_img   =   imagecreate(40,17);
    $c_black   =   ImageColorAllocate($h_img,   0,0,0);
    $c_white   =   ImageColorAllocate($h_img,   255,255,255);
    imageline($h_img,   1,   1,   350,   25,   $c_black);
    imagearc($h_img,   200,   15,   20,   20,   35,   190,   $c_white);
    imagestring($h_img,   5,   2,   1,   $login_check_number,   $c_white);
    ImagePng($h_img);
    ImageDestroy($h_img);
    die();  
    ?>html代码是:画红色的地方应该如何表达阿 谢谢了
    <script language="JavaScript">function valide(){
    if (userName.value == ""){
    alert("The username is not allow Null!");
    return false;
    }
    if (passWord.value == ""){
    alert("The password is not allow Null!");
    return false;
    }
    if (repass.value == ""){
    alert("The repassword is not allow Null!");
    return false;
    }
    if (repass.value == ""){
    alert("The repassword is not allow Null!");
    return false;
    }
    if (email.value == ""){
    alert("The Email is not allow Null!");
    return false;
    }
    if (phoneNumber.value == ""){
    alert("The PhoneNumber is not allow Null!");
    return false;
    }
    if (road.value == ""){
    alert("The Road is not allow Null!");
    return false;
    }
    if (zipcode.value == ""){
    alert("The zipcode is not allow Null!");
    return false;
    }
    if (insecCode.value == ""){
    alert("The InsecCode is not allow Null!");
    return false;
    }
    if (captcha_code.value == "" )
    {
    alert("The captcha is allow Null!");
    return false;
    }
    if (repass.value != passWord.value){
    alert("Two passwords are not the same!");
    return false;
    }
    if (captcha_code.value != Reflash.value )
    {
    alert("The captcha is not correct!");
    return false;
    }
    }
    </script>
        </head>
            <body>
               <div>
                  <fieldset>
                     <legend>Login</legend>
                        <form action="regstinfo.php" method="post" onsubmit="return valide()"><p>
                           <label for="userName" class="label">UserName:</label>
                              <input id="userName" name="userName" type="text" class="input" /></p>
                                 <p>
                                    <span>(must be 3-15 long with letters,numbers or '_')</span></p>
                                 <p>
                           <label for="passWord" class="label">passWord:</label>
                              <input id="passWord" name="passWord" type="passWord" class="input" /></p><p>
                                     <span>(must between 3 and 15 units)</span></p>
                                 <p>
                           <label for="repass" class="label">passWord again:</label>
                              <input id="repass" name="repass" type="passWord" class="input" ></p>
      <div id="t_e"></div>
                                 <p>
                           <label for="firstName" class="label">FirstName:</label>
                              <input id="firstName" name="firstName" type="text" class="input" /></p><p>
                                 <span>must be full</span></p>
                                 <p>
                           <label for="LastName" class="label">LastName:</label>
                              <input id="lastName" name="lastName" type="text" class="input" /></p><p>
                                 <span>must be full</span></p>
                                 <p>
                           <label for="gender" class="label">Gender:</label>
                            <input id="Male" name="gender" type="radio" value="Male" checked>Male
                                 <input id="Female" name="gender" type="radio" value="Female">Female</p>
                            <p>
                           <label for="type" class="label">type:</label>
                                 <input id="Client" name="type" type="radio" value="Client" checked>Client
                                 <input id="Farmer" name="type" type="radio" value="Farmer">Farmer</p>
                                 <p>
                           <label for="phoneNumber" class="label">PhoneNumber:</label>
                              <input id="phoneNumber" name="phoneNumber" type="text" class="input" /></p><p>
                                 <span>must be full</span></p>
                                 <p>
                           <label for="road" class="label">Road:</label>
                              <input id="road" name="road" type="text" class="input" /></p><p>
                                 <span>must be full</span></p>
                                 <p>
                           <label for="zipcode" class="label">Zipcode:</label>
                              <input id="zipcode" name="zipcode" type="text" class="input"></p><p>
               <span>must be full</span></p>
                                 <p>
                           <label for="insecCode" class="label">InsecCode:</label>
                              <input id="insecCode" name="insecCode" type="text" class="input" /></p><p>
                                 <span>must be full</span></p>
                                 <p>
                           <label for="email" class="label">Email:</label>
                              <input id="email" name="email" type="text" class="input" /></p><p>
                                 <span>must be full</span></p>                  
                                  <p>
      <td>Verification Code:</td>
                             <input type="text" name="captcha_code" id="captcha_code" size="10" maxlength="6" /> </td> <img id="captcha" src="checkNumber.php" alt="Reflash">
            <a href="#" onclick="document.getElementById('captcha').src = 'checkNumber.php?' + Math.random(); return false">Reload Image</a>                   
         </p>
                     
                              <input type="submit" name="submit" value="SUBMIT" class="button" onclick="valide()">
                              <input type="reset" name="reset" value="RESET" class="button">
                                 </p>
                </form>
             </fieldset>
          </div>
       </body>
    </html>
      

  8.   

    这个验证码也需要ajax提交到服务器校验才行,因为session值是保存在服务器端的
    把<input type="text" name="captcha_code" id="captcha_code" size="10" maxlength="6" />输入的内容ajax提交到服务器端,然后和服务器端的$_session["Checknum"]做一下比较就行了
      

  9.   

    用submit,下面是我自己之前做的一个页面的例子<?php include("check_login.php"); ?>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>低调走过购物系统</title>
        <link rel="stylesheet" type="text/css" href="css/font.css">
        <style type="text/css">
        <!--
         .style1 {color: #FFFFFF}
        -->
        </style>
    </head><script language="javascript">
    function chkinput(form)
    {
    if(form.title.value=="")
    {
    alert("请输入公告主题!");
    form.title.select();
    return false;
    }
    if(form.content.value=="")
    {
    alert("请输入公告内容!");
    form.content.select();
    return false;
    }
    return true;
    }
    </script><body topmargin="0" leftmargin="0" bottommargin="0">
        <table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td height="20" bgcolor="#FFCF60"><div align="center" class="style1">添加公告</div></td>
            </tr>
            
            <tr>
                <td height="175" bgcolor="#666666">
                    <table width="750" height="175" border="0" align="center" cellpadding="0" cellspacing="1">
                        <form name="form1" method="post" action="savenewgonggao.php" onSubmit="return chkinput(this)">
                            <tr>
                                <td width="80" height="25" bgcolor="#FFFFFF"><div align="center">公告主题:</div></td>
                                <td width="667" bgcolor="#FFFFFF"><div align="left"><input type="text" name="title" size="50" class="inputcss"></div></td>
                            </tr>
                            <tr>
                                <td height="125" bgcolor="#FFFFFF"><div align="center">公告内容:</div></td>
                                <td height="125" bgcolor="#FFFFFF"><div align="left"><textarea name="content" rows="8" cols="70"></textarea></div></td>
                            </tr>
                            <tr>
                                <td height="25" colspan="2" bgcolor="#FFFFFF"><div align="center"><input type="submit" value="添加" class="buttoncss">&nbsp;&nbsp;<input type="reset" value="重写" class="buttoncss"></div></td>
                            </tr>
                        </form>
                    </table>
                </td>
            </tr>
        </table>
    </body>
    </html>
      

  10.   

    $_session["Checknum"]=$num;
    应改为
    $_SESSION["Checknum"]=$num;验证码的验证应在服务器端进行,否则就失去意义
      

  11.   

    谢谢大家我问题解决了 谢谢了 我把验证都加到php里面了
    只把空值验证放在html里面