【发布时间】:2015-02-23 04:07:04
【问题描述】:
当我提交表单时,它会执行所有操作,但是当我在其他文本框中包含表单验证时,它不会收听,只会检查 usernameTxt 文本框,仅此而已。
我尝试过使用 JavaScript,但我不知道如何使用 JS 进行验证,而是使用 PHP 进行处理。
如果有人可以帮忙,请说!
这是我的代码:
<?php
//Used for stopping users seeing an errors only for our eyes.
//commented out when published
error_reporting(-1);
ini_set('display_errors', 'On');
include 'includes/dbconnect.php';
session_start();
$error = "";
$username = $_POST['usernameTxt'];
$password = sha1($_POST['passwordTxt']);
$cnfrmPass = sha1($_POST['cnfrmPasswordTxt']);
$email = $_POST['emailTxt'];
$cnfrmEmail = $_POST['cnfrmEmailTxt'];
$fName = $_POST['fNameTxt'];
$lName = $_POST['lNameTxt'];
try{
if(isset($_POST['submitBn'])){
if(!empty($username)){
$query = dbConnect()->prepare("INSERT INTO usersInfo (Username, Password, Email, FirstName, LastName) VALUES (:username, :password, :email, :fName, :lName)");
$query->bindParam(':username', $username);
$query->bindParam(':password', $password);
$query->bindParam(':email', $email);
$query->bindParam(':fName', $fName);
$query->bindParam(':lName', $lName);
if($query->execute()){
header('Location: signin.php');
}else{
$error = "ERROR!";
}
}else{
$error = "Username cannot be empty!";
}
}else{
$error = "<h3>Form cannot be empty!</h3>";
}
}catch(PDOException $e){
$error = $e->getMessage();
}
?>
<?php
include 'includes/head.php';
include 'includes/header.php';
?>
<div class="wrapper style2" style="margin-top: 20px;">
<article id="work">
<header>
<h2>Register Here.</h2>
<h3><?php echo $error; ?> </h3>
<p>If you don't have an account why not <a href="register.php">register</a>?</p>
</header>
<div class="container">
<div class="row">
<div class="4u">
<section class="box style1">
<form name="regForm" action="register.php?attempt" method="post">
<label for="username" >Username</label>
<input type="text" name="usernameTxt" style="margin-top: 5px; margin- bottom:5px;"/>
<label for="password" >Password</label>
<input type="password" name="passwordTxt" style="margin-top: 5px; margin-bottom:5px;"/>
<label for="cnfrmPass" >Password Confirm</label>
<input type="password" name="cnfrmPasswordTxt" style="margin-top: 5px; margin-bottom:5px;"/>
<label for="email" >Email</label>
<input type="email" name="emailTxt" style="margin-top: 5px; margin-bottom:5px;"/>
<label for="cnfrmEmail" >Confirm Email</label>
<input type="email" name="cnfrmEmailTxt" style="margin-top: 5px; margin-bottom:5px;"/>
<label for="fName" >First Name</label>
<input type="text" name="fNameTxt" style="margin-top: 5px; margin-bottom:5px;"/>
<label for="lName" >Last Name</label>
<input type="text" name="lNameTxt" style="margin-top: 5px; margin-bottom:5px;"/>
<input type="submit" name="submitBn" style="margin-top: 5px; margin-bottom:5px;"/>
</form>
</section>
</div>
</div>
</div>
</article>
</div>
<?php include 'includes/footer.php'; ?>
【问题讨论】:
-
请不要以明文形式发送密码,您可以在网上找到
sha1和md5的哈希函数! phpjs.org/functions/sha1 -
我没有发送纯文本密码,是吗?我以为我说它在顶部是 sha1 加密。 IE。 '$password = sha1($_POST['passwordTxt']);
-
此代码在您的 HTML 文件中吗?如果是,请提供更相关的编码,包括表格!
-
给出你的整个html代码
-
注册表代码如下
标签: javascript php forms validation pdo