简体   繁体   English

提交失败

[英]Onsubmit not working

I have this form and I tried to make a "onsubmit" that when I click submit it checks if the "email" is = to "cemail" and if username was taken before or not i got this so far 我有这个表格,我试图做一个“提交”,当我单击“提交”时,它检查“电子邮件”是否为“ cemail”,以及是否使用了用户名,到目前为止

<form class="form-horizontal" action="#" method="post" onsubmit="return ValidationEvent()">
                <fieldset>
                    <legend>SIGN UP! <i class="fa fa-pencil pull-right"></i></legend>
 <div class="form-group">
    <div class="col-sm-6">
        <input type="text" id="firstName" placeholder="First Name" class="form-control" name="firstname" autofocus required>
    </div>
    <div class="col-sm-6">
        <input type="text" id="lastname" placeholder="Last Name" class="form-control" name="lastname" autofocus required>
    </div>
</div>

                <div class="form-group">
                    <div class="col-sm-12">
                        <input type="email" id="email" placeholder="Email" name="email" class="form-control" required>
                    </div>
                </div>
              <div class="form-group">
                    <div class="col-sm-12">
                        <input type="email" id="cemail" placeholder=" Re-enter Email" name="cemail" class="form-control" required>
                    </div>
                </div>

                 <div class="form-group">
                    <div class="col-sm-12">
                        <input type="text" id="username" placeholder=" Username" name="username" class="form-control" required>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-12">
                        <input type="password" id="password" placeholder="Password" name="password" class="form-control" required>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-12">
                        <input type="text" id="datepicker" placeholder= "DOB" name="birthday" class="form-control" required>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-1"></label>
                    <div class="col-sm-8">
                        <div class="row">

                                <label class="radio-inline">
                                    <input type="radio" id="radio" value="Female" name= "gender" required>Female
                                </label>


                                <label class="radio-inline">
                                    <input type="radio" id="radio" value="Male" name= "gender">Male
                                </label>

                        </div>
                    </div>
                </div> <!-- /.form-group -->
                <div class="form-group">
                    <div class="col-sm-4 col-sm-offset-3">
                        <button type="submit" class="btn btn-primary btn-block">Register</button>
                    </div>
                </div>
            </form>

Javascript code: JavaScript代码:

 <script>
  function ValidationEvent() {
      var email = document.getElementById("email").value;
        var username = document.getElementById("username").value;
        var cemail = document.getElementById("cemail").value;
        // Conditions
        if (email.match != cemail.match) {
            alert("Your email doesn't match!");

        }
        if(mysqli_num_rows($result) != 0)
        {
            alert("Username already taken!");
        }
        else {
        alert("Thank you");
        }

        }
  </script>

Am I approaching the function in the wrong way is there another easier way and is it okay i put an sql statement in my java script ? 我是否以错误的方式使用该函数,是否还有另一种更简便的方法,可以在我的Java脚本中放置sql语句吗?

For checking the emails with email & cemail use 用于检查带有电子邮件和cemail的电子邮件
email.localeCompare(cemail)
This will check the string comparison betwwen two emails 这将检查两个电子邮件之间的字符串比较

And for mysqli_num_rows , is not defined any where in javascript, so we will get the undefined error in console, so need to write a different funnction with that name. 对于mysqli_num_rows,在javascript中未定义任何地方,因此我们将在控制台中得到未定义的错误,因此需要使用该名称编写一个不同的函数。

First, don't use inline HTML event handling attributes (like "onsubmit") as they create "spaghetti code", anonymous global event handling wrapper functions and don't conform to the modern W3C DOM Event handling standard . 首先,不要使用内联HTML事件处理属性(例如“ onsubmit”),因为它们会创建“意大利面条代码”,匿名全局事件处理包装函数,并且不符合现代W3C DOM事件处理标准

Second, your .php results have to be gotten from somewhere. 其次,您的.php结果必须从某个地方获取。 You'll need to put a call into that file for its results before you can use them. 您需要先调用该文件以获取结果,然后才能使用它们。

Next, you were using the .match() string method incorrectly to compare the emails against each other. 接下来,您错误地使用了.match()字符串方法来相互比较电子邮件。 All you really need to do is compare the values entered into the email fields (it's also a good idea to call .trim() on form values to strip out any leading or trailing spaces that might have been inadvertently added). 您真正需要做的就是比较在电子邮件字段中输入的值.trim()对表单值调用.trim().trim()可能无意添加的任何前导或尾随空格也是一个好主意)。

Once you restructure your code to use standards, the JavaScript will change as follows (FYI: This won't work in the Stack Overflow snippet environment because form submissions are blocked, so you can see a working version here ): 重组代码以使用标准后,JavaScript将会进行如下更改(仅供参考:由于表单提交被阻止,因此在Stack Overflow代码段环境中将无法使用,因此您可以在此处查看有效的版本):

 // When the DOM is loaded: window.addEventListener("DOMContentLoaded", function(){ // Get references to the DOM elements you will need: var frm = document.getElementById("frm"); // Don't set variables to the values of DOM elements, // set them to the DOM elements themselves so you can // go back and get whatever properties you like without // having to scan the DOM for them again var email = document.getElementById("email"); var username = document.getElementById("username"); var cemail = document.getElementById("cemail"); // Set up a submit event handler for the form frm.addEventListener("submit", validationEvent); // All DOM event handling funcitons receive an argument // that references the event they are responding to. // We need that reference if we want to cancel the event function validationEvent(evt) { // Conditions if (email.value.trim() !== cemail.value.trim()) { alert("Your email doesn't match!"); // Cancel the form submit event evt.preventDefault(); evt.stopPropagation(); return; } // You need to have already gotten the "mysqli_num_rows($result)" value // from your .php file and saved it to a variable that you can then check // here against "!=0" if(mysqli_num_rows($result) != 0) { alert("Username already taken!"); // Cancel the form submit event evt.preventDefault(); evt.stopPropagation(); } else { alert("Thank you"); } } }); 
 <form class="form-horizontal" id="frm" action="#" method="post"> <fieldset> <legend>SIGN UP! <i class="fa fa-pencil pull-right"></i></legend> <div class="form-group"> <div class="col-sm-6"> <input type="text" id="firstName" placeholder="First Name" class="form-control" name="firstname" autofocus required> </div> <div class="col-sm-6"> <input type="text" id="lastname" placeholder="Last Name" class="form-control" name="lastname" autofocus required> </div> </div> <div class="form-group"> <div class="col-sm-12"> <input type="email" id="email" placeholder="Email" name="email" class="form-control" required> </div> </div> <div class="form-group"> <div class="col-sm-12"> <input type="email" id="cemail" placeholder=" Re-enter Email" name="cemail" class="form-control" required> </div> </div> <div class="form-group"> <div class="col-sm-12"> <input type="text" id="username" placeholder=" Username" name="username" class="form-control" required> </div> </div> <div class="form-group"> <div class="col-sm-12"> <input type="password" id="password" placeholder="Password" name="password" class="form-control" required> </div> </div> <div class="form-group"> <div class="col-sm-12"> <input type="text" id="datepicker" placeholder= "DOB" name="birthday" class="form-control" required> </div> </div> <div class="form-group"> <label class="control-label col-sm-1"></label> <div class="col-sm-8"> <div class="row"> <label class="radio-inline"> <input type="radio" id="radio" value="Female" name= "gender" required>Female </label> <label class="radio-inline"> <input type="radio" id="radio" value="Male" name= "gender">Male </label> </div> </div> </div> <!-- /.form-group --> <div class="form-group"> <div class="col-sm-4 col-sm-offset-3"> <button type="submit" class="btn btn-primary btn-block">Register</button> </div> </div> </form> 

First give a name and an action to your form 首先给您的表单起一个名字和一个动作

<form class="form-horizontal" id="myform" action="chkValues.php" method="post" >
....


            <div class="form-group">
                <div class="col-sm-12">
                    <input type="email" id="email" placeholder="Email" name="email" class="form-control" required>
                </div>
            </div>
          <div class="form-group">
                <div class="col-sm-12">
                    <input type="email" id="cemail" placeholder=" Re-enter Email" name="cemail" class="form-control" required>
                </div>
            </div>


....
</form>

Then put this script at the bottom 然后将此脚本放在底部

<script>
$('#myForm').on("sumbit", function(){
 // cancel the original sending
event.preventDefault(); 

$.ajax({
  var form = $(this);
  var action = form.attr("action"),
      method = form.attr("method"),
      data   = form.serialize();
})          
.done: function(data) // is called wehn the call was okay
  {
    if( data.substr(0, 5) == "Error"){
       alert(data);   // sent the sting of the "error message" begining with "Error"
      }else{
       top.location.href = data;  // sent the sting of the "success page" when all was okay and data are saved in the database
      }            
    }
.fail(function() {
   alert( "Error: Getting data from Server" );
})
});
</script>

in the php file check the values an return an error if something went wrong. 在php文件中检查值,如果出现问题,则返回错误。

<?php
  if(!isset($_POST['email']) || !isset($_POST['cemail'])){
    die("Error: Please fill out both email fields.");
    if($_POST['email'] != $_POST['cemail'] ){
      die("Error: The Email adresses do not match.");
    }
  here do what you want to do with the data. 



when finish just send the new url
echo "success.html";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM