执行PHP提交按钮仅在Javascript为真时执行

时间:2015-12-22 08:12:23

标签: javascript php html

我正在尝试使用PHP表单执行mysql查询。以下是表格

OnCreate

我在Javascript中有以下表单验证,

<html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link rel="stylesheet" href="signup.css" type="text/css">
        <script type="text/javascript" src="form_val.js"></script>
        <title>Untitled Document</title>
        </head>
        <body bgcolor="#0ca3d2">
        <div align="right">
        <h2><a href="index.php">Go back</a></h2>
        </div>
        <div class="form-style-10">
        <h1>Sign Up Now!<span>Sign up and tell us what you think of the site!</span></h1>

        <form action="#" method="post" name="myForm" onSubmit="CheckTerms()">
        <div class="section"><span>1</span>First Name &amp; Address</div>
        <div class="inner-wrap">
        <input type="text" name="First_Name" placeholder="Enter First Name" id="fname">
        <label id="error" style="color:red"></label>
        <input type="text" name="Last_Name" placeholder="Enter last Name"/>
        <label id="error_l" style="color:red"></label>
        <select name="gender">

        <option value="Male">Male</option>
        <option value="Female">Female</option>
        </select>
        </div>
        <div class="section"><span>2</span>Email </div>
        <div class="inner-wrap">
        <input type="text" name="Email_i" placeholder="enter email here" id="e_mail" />
        <label id="error_e" style="color:red"></label>
        <button type="button" name="validate" onclick="loadDoc(this.value)">Validate</button>
        <div id="check"></div></div>
        <div class="section"><span>3</span>Passwords</div>
        <div class="inner-wrap">
        <input type="password" name="pass" placeholder="Must be 6 to 15 characters" id="Pass" onBlur="PassCheck()" />
        <label id="error_p" style="color:red"></label>
        <input type="password" name="repass" placeholder="Retype Password" id="RePass"/>
        <label id="error_rp" style="color:red"></label>
        <span class="privacy-policy">
        <input type="checkbox" name="terms" value="value1" id="check_box">Agree to Terms and Conditions
        </span>
        <input type="submit" name="signup" value="Register" id="sub_button">
        <label id="error_lable" style="color:red"></label>
        </div>
        </form>
        </div>
        </body>
function CheckTerms(){
        var letter = /^[a-zA-Z ]*$/;
        if(document.myForm.First_Name.value.match(letter) && document.myForm.First_Name.value =="")
        {
        document.getElementById("error").innerHTML =  "* Please Provide First Name";
        document.myForm.First_Name.focus();
        return false;
        }


        if(document.myForm.Last_Name.value.match(letter) && document.myForm.Last_Name.value =="" )
        {
        document.getElementById("error_l").innerHTML =  "* Please provide your Last name!";
        document.myForm.Last_Name.focus() ;
        return false;
        }


        var email =/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
        if(document.myForm.Email_i.value.match(email) && document.myForm.Email_i.value=="")
        {
        document.getElementById("error_e").innerHTML =  "* Please provide your EMAIL!"; 
        document.myForm.Email_i.focus() ;
        return false;
        }


        var min = 6;
        var max = 15;

        if(document.myForm.pass.value.length < min || document.myForm.pass.value.length > max)
        {
        document.getElementById("error_p").innerHTML =  "Password Should be betweeen 6 to 15 characters"; 
        document.myForm.pass.focus() ;
        return false;
        }


        var pass = document.getElementById("Pass").value;
        var re = document.getElementById("RePass").value;

        if(pass != re)
        {
        document.getElementById("error_rp").innerHTML =  "Password do not match";             
        return false;
        }

        if(document.getElementById("check_box").checked == false)
        {
          document.getElementById("error_lable").innerHTML =  "Please Check";
          return false;             
        }}

但问题是,即使javascript返回错误,单击提交按钮,执行PHP脚本导致数据进入数据库。如果存在任何javascript错误,我想停止表单提交。

1 个答案:

答案 0 :(得分:8)

即使您在函数中执行此操作,也不会在事件中返回布尔值。 onSubmit默认收到true并提交。

更改

onsubmit="CheckTerms()"

 onsubmit="return CheckTerms()"