1

我有我的 html 页面,人们可以在其中注册,它调用我的 php 页面来检查和输入到 db,如果我收到错误,例如有人没有选择他们的性别,它会提醒用户应该这样做。问题是,如果用户没有选择性别并再次提交,那么现在调用 ajax 两次,我收到两条相同的警报消息,如果再次完成,我会收到三个警报消息,并且每次按下都会继续增长等等。我怎么能阻止这种情况发生。

<!DOCTYPE HTML>
<html>
<head>
      <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <title>
        </title>
        <link rel="stylesheet" href="css/logout-button.min.css" />
        <link rel="stylesheet" href="css/jquery.mobile-1.3.0.min.css" />
        <link rel="stylesheet" href="css/my.css" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        </script>
        <script src="js/jquery.mobile-1.3.0.min.js"></script>
        <script src="js/jquery.cookies.2.2.0.min.js"></script>
        <script src="js/account-login.js"></script>

</head>
     <body>
        <!-- Home -->
        <div data-role="page" id="account-signup">
        <script>
                function submitForm() {
                    $(document).ready(function() {
                        $("form#signupForm").submit(function() {
                            var form_data = $('#signupForm').serialize();
                            $.ajax({
                            type: "POST",
                            url: "ajaxResponder.php",
                            dataType: 'html',
                            data: form_data,
                            complete: function(data){
                                if(data.responseText == 'yes'){
                                alert("Your Account has been created");
                                $.mobile.changePage( "account-login.html", { transition: "slide"} );
                                }else {
                                alert(data.responseText);
                                }
                            }
                        });
                    return false;
                    });
                });
                }
            </script>
            <div data-theme="a" data-role="header">
                <a data-role="button" href="account-login.html" data-transition="slide"
                data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Back
                </a>
                <h3>
                   Sign-Up
                </h3>
            </div>
            <div data-role="content">
                <form id="signupForm" name="signupForm">
                <input name="method" id="method" placeholder="method" value="account-signup" type="hidden" />
                    <div data-role="fieldcontain">
                        <fieldset data-role="controlgroup">
                            <label for="forename">
                                Forename *
                            </label>
                            <input name="forename" id="forename" placeholder="Forename" value="" type="text" />
                        </fieldset>
                    </div>
                    <div data-role="fieldcontain">
                        <fieldset data-role="controlgroup">
                            <label for="surname">
                                Surname *
                            </label>
                            <input name="surname" id="surname" placeholder="Surname" value="" type="text" />
                        </fieldset>
                    </div>

                    <div data-role="fieldcontain">
                    <label for="gender">Gender</label>
                    <select name="gender" id="gender" data-native-menu="false">
                    <option>Gender</option>
                    <option value="male">Male</option>
                    <option value="female">Female</option>
                    </select>
                    </div>
                    <div data-role="fieldcontain">
                        <script type="text/javascript">

                                $(function(){
                                    var items="";
                                    $.getJSON("ajaxResponder.php?method=account-signup-countries",function(data){
                                    items+='<option value="Select Country">Select Country</option>';
                                    $.each(data,function(index,item) 
                                    {
                                    items+="<option value='"+item.id+"'>"+item.name+"</option>";
                                     });
                                    $("#countries").html(items); 
                                    $("#countries").trigger("change");
                                    });
                                });

                        </script>
                        <legend>Country *</legend>
                        <select name="countries" id="countries" data-native-menu="false">
                        </select>
                    </div>
                    <div data-role="fieldcontain">
                        <fieldset data-role="controlgroup">
                            <label for="email">
                                Email *
                            </label>
                            <input name="email" id="email" placeholder="Email" value="" type="text" />
                        </fieldset>
                    </div>
                    <div data-role="fieldcontain">
                        <fieldset data-role="controlgroup">
                            <label for="password">
                                Password *
                            </label>
                            <input name="password" id="password" placeholder="Password" value="" type="password" />
                            <label for="confirm">
                                Confirm Password *
                            </label>
                            <input name="confirm" id="confirm" placeholder="Confirm" value="" type="password" />
                        </fieldset>
                    </div>
                    <input data-theme="b" value="Sign-Up" type="submit" onclick="submitForm()" />
                </form>
            </div>
        </div>
    </body>
</html>

我的PHP

 case 'account-signup':
    $password_minlength = 6;
    $password_maxlength = 40;

    $message == "";

function validusername($username) {
        $allowedchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'";
        for ($i = 0; $i < strlen($username); ++$i)
            if (strpos($allowedchars, $username[$i]) === false)
            return false;
        return true;
}
    $forename= $_POST["forename"];
    $surname= $_POST["surname"];
    $email= $_POST["email"];
    $password= $_POST["password"];
    $confirm= $_POST["confirm"];
    $day = $_POST["select-choice-day"];
    $month = $_POST["select-choice-month"];
    $year = $_POST["select-choice-year"];
    $country = $_POST["countries"];
    $gender = $_POST["gender"];

  if ($day =='Day' or $month == 'Mon' or $year =='Year')
    $message = "You Need to fill in Your DOB";
  if ($gender =='Gender')
    $message ="Please select your Gender";
  if ($country == "Select Country")
    $message ="Please select a Country";

  if (empty($password) || empty($forename) || empty($surname) || empty($email) || empty($confirm)  )
    $message = "All fields required to be filled in";

  elseif ($password != $confirm)
    $message = "Your passwords do not match";
  elseif (strlen($password) < $password_minlength)
    $message = sprintf("Your password needs to be more than $password_minlength char");
  elseif (strlen($password) > $password_maxlength)
    $message = sprintf("Your password needs to be less than $password_maxlength char");
  elseif (!validusername($forename))
    $message = "Invalid characters used in your forename";
    elseif (!validusername($surname))
    $message = "Invalid characters used in your surname";
  elseif (!validemail($email))
        $message = "A Valid email address is required";

    if ($message == "") {
          // check if email addy is already in use
          $a = (@mysql_fetch_row(@SQL_Query_exec("select count(*) from users where email='$email'")));
          if ($a[0] != 0) 
            $message = "Email Address $email has already signed up. Please use account-recovery ";          
        }
  if ($message == "") {
    $signupclass = '1';
    $status = "confirmed";
    $dob = "$year-$month-$day";
    $secret = mksecret(); //generate secret field
    $password = passhash($password);// hash the password
    $forename1 = str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($forename))));
    $surname1 = str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($surname))));
    SQL_Query_exec("INSERT INTO users (forename,surname, password, secret, email, status, added, last_access, country, gender,dob, stylesheet, language, class, ip) VALUES (" .
      implode(",", array_map("sqlesc", array($forename1,$surname1, $password, $secret, $email, $status, get_date_time(), get_date_time(), $country, $gender,$dob, $site_config["default_theme"], $site_config["default_language"], $signupclass, getip()))).")");
      $message="yes";
  }
echo "$message";
  break;
4

1 回答 1

2

您的 javascript 中有错误,调用函数时,您不需要这些行:

$(document).ready(function() {
    $("form#signupForm").submit(function() {

首先,您不需要准备好文档,因为您不需要等待该状态。第二件事,如果您使用带有 onclick=... 的内联 hjavascript,则不需要将提交事件绑定到表单。

基本上每次您再次调用时,您都会将提交事件绑定到表单。

第三件事,如果验证是在客户端实现的,这不会发生,所以考虑一下。您可以轻松实现jQuery Validate插件。

我对你的建议是,从这一行中删除 onclick 事件:

<input data-theme="b" value="Sign-Up" type="submit" onclick="submitForm()" />

删除 onclick 函数,然后使用如下所有内容:

$(document).ready(function() {
    $('#signupForm').off('submit').on('submit',function(e) {
        var form_data = $('#signupForm').serialize();
        $.ajax({
            type: "POST",
            url: "ajaxResponder.php",
            dataType: 'html',
            data: form_data,
            complete: function(data){
                if(data.responseText == 'yes'){
                    alert("Your Account has been created");
                    $.mobile.changePage( "account-login.html", { transition: "slide"} );
                }else {
                    alert(data.responseText);
                }
            }
        });
        return false;
    });
});

由于提交事件,当您按下提交按钮时,此代码将触发,无需点击。并注意现在如何处理提交:

$('#signupForm').off('submit').on('submit',function(e) {

这将防止发生多个提交事件。

于 2013-04-30T20:58:05.097 回答