未单击提交按钮

时间:2014-11-03 20:16:38

标签: javascript php html

我正在尝试使用php脚本验证表单。 验证完全正常,但如果我提交了正确的详细信息,则不会单击该按钮。 当我不输入任何细节时,显示所需字段的消息。 当我输入错误的详细信息时,会显示警告消息。 但是当我输入正确的详细信息时,不会单击登录按钮。 在alls()函数中,我尝试返回true,然后在显示所需的字段消息一秒后刷新它的问题。

HTML code:

<form id="frm_login" method="post" name="frm_login" onSubmit="return alls()">

      UserName: <input type="text" name="txt_usrnm" /><label id="i"></label>
      <br/><br/>
      Password: <input type="password" name="pswd" /><label id="i1"></label>
      <br/><br/>

       <input type="submit" name="submit" value="Login" style:"width=10px"/>&nbsp;&nbsp;&nbsp;
      <a href="forgotPassword.php">Forgot Password ?</a>

      <br/><br/>
      <font size="+1"><a href="reg.html">Register Here</a></font>
    </form>

使用Javascript:

<script type="text/javascript">
function req() 
{

if (document.frm_login.txt_usrnm.value=="") 
{

    document.getElementById('i').innerHTML="*This field is required";
    document.getElementById('i').style.color="red";
    document.getElementById('i').style.fontSize="12px";
}

if (document.frm_login.pswd.value=="") 
{

    document.getElementById('i1').innerHTML="*This field is required";
    document.getElementById('i1').style.color="red";
    document.getElementById('i1').style.fontSize="12px";
}
return false;
}
function validateUname() 
{
submitFlag = true;
var len=document.frm_login.txt_usrnm.value.length;
if((len>0) && (len<6)){
    submitFlag=false;
    document.getElementById('i2').innerHTML="*Enter atleast 6 characters";
    document.getElementById('i2').style.color="red";
    document.getElementById('i2').style.fontSize="12px";
}
return submitFlag;
}
function alls()
{
req();
validateUname();
//CheckPassword(this);
//num();
//confirm_pswd();
//namevalid();
//ValidateEmail(this);  
return false;
}

</script>

PHP代码:

<?php
      if(isset($_POST['submit']))
{
 $usrnm1=$_POST['txt_usrnm'];
 $pswd1=$_POST['pswd'];

 $user_name = "root";
 $password = "";
 $database = "show_your_talent";
 $server = "127.0.0.1";
 $db_handle = mysql_connect($server, $user_name, $password);
 $db_found = mysql_select_db($database, $db_handle);

 $res="select * from username where UserName='$usrnm1' and Password='$pswd1'";
 $result2 = mysql_query($res,$db_handle);   


    $count=mysql_num_rows($result2);
    if($count==1)
    {   

         $_SESSION['user'] =$usrnm1;
         //echo $_SESSION['user'];
         header("Location: category.php");   

   }
    else
    {
        //$_SESSION['user']="false";
        echo "<script type='text/javascript'> alert('Incorrect UserName/Password.')</script>"; 
        //header('Location: index.php');            
    }
    mysql_close($db_handle);

  }
  ?>

2 个答案:

答案 0 :(得分:1)

您提交函数alls(),始终返回false,表示表单不会提交。试试这个:

function req() {
    var submitFlag = true;
    if (document.frm_login.txt_usrnm.value == "") {
        submitFlag = false;

        document.getElementById('i').innerHTML = "*This field is required";
        document.getElementById('i').style.color = "red";
        document.getElementById('i').style.fontSize = "12px";
    }

    if (document.frm_login.pswd.value == "") {
        submitFlag = false;

        document.getElementById('i1').innerHTML = "*This field is required";
        document.getElementById('i1').style.color = "red";
        document.getElementById('i1').style.fontSize = "12px";
    }
    return submitFlag;
}

function validateUname() {
    submitFlag = true;
    var len = document.frm_login.txt_usrnm.value.length;
    if ((len > 0) && (len < 6)) {
        submitFlag = false;
        document.getElementById('i').innerHTML = "*Enter atleast 6 characters";
        document.getElementById('i').style.color = "red";
        document.getElementById('i').style.fontSize = "12px";
    }
    return submitFlag;
}

function alls() {
    var valid = true;
    valid *= req();
    valid *= validateUname();

    //CheckPassword(this);
    //num();
    //confirm_pswd();
    //namevalid();
    //ValidateEmail(this);  

    return valid ? true : false;
}

会阻止在req()validateUname()返回false时提交表单。

答案 1 :(得分:0)

alls()始终返回false。因此,您永远不会提交表格。 当onsubmit回调返回false时,将停止向服务器的提交。