简体   繁体   English

我正在尝试通过使用jqueryValidation引擎插件来验证注册表单,某些验证无法正常工作

[英]I am trying to validate a signup form by using jqueryValidation engine plugin some validations are not working correctly

First of all I have to tell that I am new to php. 首先,我必须告诉我我是php新手。 I am trying to validate a signup form by using jqueryValidation engine plugin. 我正在尝试通过使用jqueryValidation引擎插件来验证注册表单。 I succeed on validating some parts and now I am stuck at the user name validation part (check whether the name user entered is already in the database and if it is, display 'username is already taken'). 我成功验证了某些部分,现在我停留在用户名验证部分(检查用户输入的名称是否已经在数据库中,如果是,则显示“用户名已被使用”)。 When i enter a username 'validating please wait' prompt is displaying and I can't submit the form. 当我输入用户名时,显示“正在验证,请稍候”提示,并且我无法提交表单。 I have used bootstrap 3.3.6. 我用过bootstrap 3.3.6。

Here is my signup form 这是我的注册表格

<div class="modal fade" id="signupForm" data-backdrop="false" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content Popmodal">
            <div class="modal-header">
                <button type="button" class="close Popclose" data-dismiss="modal" aria-label="">
                    <span>&times;</span>
                </button>
                <h4 class="modal-title Poptit">Sign Up</h4>
            </div>

            <div class="modal-body Popbody">
                <div id="error">
                    <!-- error will be showen here ! -->
                </div>
                <form class="form-horizontal" method="post" action="registration.php" id="reg-form">
                    <div class="form-group"><br>
                        <label class="col-md-4 col-md-offset-1">User Name :</label>
                        <div class="col-md-5">
                            <input type="text" placeholder="A name that you like to use" value = "" class="form-control input-sm popname validate[required,custom[onlyLetterNumber],maxSize[20],ajax[ajaxUserCallPhp]]" name="uname" id="uname" data-errormessage-value-missing="Username is required !" data-prompt-position="bottomLeft:5,4"><br>

                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-4 col-md-offset-1">Email :</label>
                        <div class="col-md-5">
                            <input type="email" placeholder="Your email address" class="form-control input-sm popname validate[required,custom[email]]" name="mail" id="mail" data-errormessage-value-missing="Email is required !"  data-errormessage-custom-error="Invalid email address !" data-prompt-position="bottomLeft:5,4"><br>
                        </div>

                    </div>

                    <div class="form-group">
                        <label class="col-md-4 col-md-offset-1">Password :</label>
                        <div class="col-md-5">
                            <input type="password" maxlength="15" placeholder="Password" class="form-control input-sm popname validate[required]" name="pass" id="pass" data-errormessage-value-missing="Password is required !"  data-prompt-position="bottomLeft:5,4"><br>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-4 col-md-offset-1">Confirm Password :</label>
                        <div class="col-md-5">
                            <input type="password" maxlength="15" placeholder="Confirm password" class="form-control input-sm popname validate[required,equals[pass]]" name="cpass" id="cpass" data-errormessage-value-missing="Confirm password is required !" data-errormessage-pattern-mismatch="Password fields do not match !" data-prompt-position="bottomLeft:5,4"><br>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-2 col-md-offset-8">
                            <button type="submit" class="btn btn-success sign" value="submit" name="signup">Sign Up</button>
                        </div>
                    </div>
                 </form>
            </div>
        </div>
    </div>
</div>  

Here is the js part 这是js部分

  <script>
    $(document).ready(function(){
    $("#reg-form").validationEngine();
  });
  </script> //this part is on the index page with signup form

"ajaxUserCallPhp": {
                "url": "ajaxValidateFieldUser.php",
                "extraDataDynamic": ['#uname'],
                "alertTextOk": "* This username is available",
                "alertText": "* This user is already taken",
                "alertTextLoad": "* Validating, please wait"
            }

Here is the php code 这是PHP代码

<?php

  $validateValue=$_REQUEST['fieldValue']; 
  $validateId=$_REQUEST['uname'];


  $validateError= "This username is already taken";
  $validateSuccess= "This username is available";

   $arrayToJs = array();
   $arrayToJs[0] = $validateId;

   require_once('dbconfig.php');

   $stmt = $db_con->query('SELECT uname FROM users WHERE uname    ='$validateValue'');
   $stmt->bindValue(':uname', $uname, PDO::PARAM_STR);
   $stmt->execute();
   $res = $stmt->fetchAll(PDO::FETCH_ASSOC);

   if ($res->fetchColumn() > 0) { 
   $arrayToJs[1] = false;
   echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR

   } else{

    $arrayToJs[1] = true;
    echo json_encode($arrayToJs); // RETURN ARRAY WITH success
   }

  ?>

Let's start here: 让我们从这里开始:

$stmt = $db_con->query('SELECT uname FROM users WHERE uname    = '$validateValue'');
$stmt->bindValue(':uname', $uname, PDO::PARAM_STR);
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);

Your quotes are completely broken, so the file won't even parse. 您的报价完全被打断了,因此该文件甚至不会解析。 Even with that fixed, there are still many problems. 即使修复了该问题,仍然存在许多问题。

Here is some documentation on how to do a basic query. 这是一些有关如何进行基本查询的文档 In short, prepare then execute. 简而言之,准备然后执行。 Binding is generally un-necessary for simple SELECT queries, and calling PDO::query() doesn't do anything at all with prepared statements. 对于简单的SELECT查询,通常不需要绑定,而对PDO::query() ,对准备好的语句根本不做任何事情。 Try this instead: 尝试以下方法:

$stmt = $db_con->prepare('SELECT uname FROM users WHERE uname = :uname');
$stmt->execute(["uname"=>$validateValue]);

Then PDOStatement::fetchAll() returns an array, but you are trying to call PDOStatement::fetchColumn() on it. 然后PDOStatement::fetchAll()返回一个数组,但是您尝试在其上调用PDOStatement::fetchColumn() Pick one or the other. 选择一个或另一个。

There are further problems, but you should take a look at your code line by line and understand what each line does. 还有其他问题,但是您应该逐行查看代码并了解每一行的功能。 You can't hope to copy and paste things from a half dozen websites and have it work. 您不能希望从六个网站上复制并粘贴内容并使其正常运行。

暂无
暂无

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

相关问题 我正在尝试使用JavaScript中的正则表达式验证日期,但无法正常工作? - I am trying to validate a date with a regular expression in JavaScript but it is not working correctly? 我试图验证一个简单的HTML表单 - I am trying to validate a simple html form 我正在尝试使用 javaScript 验证移动设备,但它不起作用 - I am trying to validate mobile with javaScript but it not working 我正在尝试使用JavaScript验证用户输入。 我想验证用户是否正确输入了他们的课程名称 - I am trying to validate user input using JavaScript. I would like to validate that the user has input their the title of their course correctly 使用指令无效的表单验证 - form validations using a directive not working 我使用了与验证电子邮件名称相同的逻辑,但是由于某种原因,它无法正常工作 - I am using the same logic that I used to validate the name to the email but for some reason it's not working 尝试使用ember-cp-validations验证表单时出现“ TypeError:用户为空” - Getting 'TypeError: user is null' when trying to validate form using ember-cp-validations onsubmit 表单不工作需要显示一些验证 - onsubmit form not working need to show some validations 我正在尝试使用 jquery 对表单变量设置操作。 它正确设置了动作,但在动作页面上没有任何内容 - I am trying to set action with variable of the form using jquery. It is correctly setting the action, but on the action page there is nothing charCodeAt()可用于某些验证,但不能用于其他验证 - charCodeAt() working with some validations, but not some other validations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM