首先,我必须告诉我我是php的新手。我正在尝试使用jqueryValidation引擎插件验证注册表单。我成功验证了一些部分,现在我被困在用户名验证部分(检查用户输入的名称是否已经存在于数据库中,如果是,则显示'用户名已被采用')。当我输入用户名'验证时请等待'提示正在显示,我无法提交表单。我使用过bootstrap 3.3.6。
这是我的注册表格
<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>×</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>
这是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"
}
这是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
}
?>
答案 0 :(得分:0)
让我们从这里开始:
-l
您的报价已完全损坏,因此该文件甚至无法解析。即使有了这个问题,仍然存在许多问题。
以下是关于如何进行基本查询的documentation。简而言之,准备然后执行。对于简单的$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);
查询,绑定通常是不必要的,并且调用SELECT
对预处理语句完全没有任何作用。试试这个:
PDO::query()
然后$stmt = $db_con->prepare('SELECT uname FROM users WHERE uname = :uname');
$stmt->execute(["uname"=>$validateValue]);
返回一个数组,但您尝试在其上调用PDOStatement::fetchAll()
。选择其中一个。
还有其他问题,但您应该逐行查看代码并了解每行的功能。你不能希望从六个网站复制和粘贴东西并使其有效。