Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP验证仅验证表单中的第一个文本框_Javascript_Php_Forms_Validation_Pdo - Fatal编程技术网

Javascript PHP验证仅验证表单中的第一个文本框

Javascript PHP验证仅验证表单中的第一个文本框,javascript,php,forms,validation,pdo,Javascript,Php,Forms,Validation,Pdo,当我提交表单时,它会执行所有操作,但当我在其他文本框中包含表单验证时,它不会侦听,只会选中usernameTxt文本框,就这样 我尝试过使用JavaScript,但我不知道如何用JS验证,而是用PHP处理 如果有人能帮忙,请说 这是我的密码: <?php //Used for stopping users seeing an errors only for our eyes. //commented out when published error_reporting(-1); ini_s

当我提交表单时,它会执行所有操作,但当我在其他文本框中包含表单验证时,它不会侦听,只会选中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'; ?>

您需要使用检查行扩展PHP脚本。所有这些加起来。如果达到某个数字,则所有字段都是正确的,如果没有,则对不正确的字段中止。这也可以在JavaScript中完成。将为您提供两种解决方案

if ($_SERVER['REQUEST_METHOD'] == "POST") //only execute when a post is sent to register.php
{
    $username = $_POST['usernameTxt'];
    $password = sha1($_POST['passwordTxt']);
    $cnfrmPass = sha1($_POST['cnfrmPasswordTxt']);
    $email = $_POST['emailTxt'];
    $cnfrmEmail = $_POST['cnfrmEmailTxt'];
    $fName = $_POST['fNameTxt'];
    $lName = $_POST['lNameTxt'];    

    function validate()
    {
        global $username, $password, $cnfrmPass, $email, $cnfrmEmail;
        var index = 0;
        //start the check
        if (!empty($username) )
        {
            index++;
        }
        else
        {
            return "Username can't be empty";
        }
        if ( (!empty($password) && !empty($cnfrmPass) ) && $password === $cnfrmPass)
        {
            index++; //password and confirm password are not empty and equal to each other.
        }
        else
        {
            if (empty($password) || empty($cnfrmPass))
            {
                return "password can't be empty";
            }
            else
            {
                return "passwords do not match";
            }
        }
        if ( (!empty($email) && !empty($cnfrmEmail) ) && $email === $cnfrmEmail) //you also want to check if this is a correct e-mail 
        {
            index++; //email and email password are not empty and equal to each other.
        }
        else
        {
            if (empty($email) || empty($cnfrmEmail))
            {
                return "Email can't be empty";
            }
            else
            {
                return "emails do not match";
            }
        }

        return index;
    }

    $validated = validate();
    if (validated == 3)
    {
        //validation pans out --> continue.
        $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 = $validated;        
    }
}    
您还可以在此处读取的输入上使用HTML5验证:

解决方案2 JavaScript

更改提交按钮

<input type="button" name="submitBn" value="Submit" onclick="validateForm()" style="margin-top: 5px; margin-bottom:5px;"/>
添加这个css

<style type="text/css">
    .input_error {
        border: 1px solid red;
    }

    .input_good {
        border: 1px solid lime;
    }

</style>

好的,所以最后我决定不添加Javascript,这似乎有些过分了。。。我想也许只是让服务器验证一下——也许它会带来更少的安全问题

无论哪种方式,我都是这么做的:

//setting variables to use here
$error = "";
$username = $_POST['usernameTxt'];
$password = sha1($_POST['passwordTxt']);
$cnfrmPass = sha1($_POST['cnfrmPasswordTxt']);
$email = $_POST['emailTxt'];
$cnfrmEmail = $_POST['cnfrmEmailTxt'];

//checking for any empty values or incorrect values
if(empty($username) === true){
    $error = "Username cannot be empty";
}
if($password != $cnfrmPass){
    $error = "Passwords do not match";
}
if($email != $cnfrmEmail){
    $error = "Emails do not match";
}

//if no errors occur start the registration process
if(empty($error) === true){
    $selectQ = dbConnect()->prepare("SELECT `Username` FROM `usersInfo` WHERE `Username`=:username");
    //setting the variables to the query
    $selectQ->bindParam(":username", $username);
    $selectQ->execute(); //execute query

    $count = $selectQ->rowCount(); //see if the above query has found anyone
    if($count > 0){
        $error = "User already exists. Please try again.";   
    }else{
        //if no users found start the insert query
        $insertQ = dbConnect()->prepare("INSERT INTO usersInfo (Username, Password, Email) VALUES (:username, :password, :email)");
        //setting the variables to the query
        $insertQ->bindParam(':username', $username);
        $insertQ->bindParam(':password', $password);
        $insertQ->bindParam(':email', $email);

        if($insertQ->execute()){
            //if the insert query executed successfully redirect them
            //to the login in page.
            header('Location: signin.php');
        }else{
            //otherwise show this error.
            $error = "Query could not execute!";
        }
    }
}

请不要发送纯文本密码,您可以在JavaScript在线中找到sha1和md5哈希函数!我没有用明文发送密码,是吗?我想我说的是在顶部的sha1加密中。即‘$password=sha1$_POST['passwordTxt'”;这个代码在你的HTML文件中吗?如果是,请提供更多相关编码,包括表格!在下面的注册表格中给出您的整个html编码
//setting variables to use here
$error = "";
$username = $_POST['usernameTxt'];
$password = sha1($_POST['passwordTxt']);
$cnfrmPass = sha1($_POST['cnfrmPasswordTxt']);
$email = $_POST['emailTxt'];
$cnfrmEmail = $_POST['cnfrmEmailTxt'];

//checking for any empty values or incorrect values
if(empty($username) === true){
    $error = "Username cannot be empty";
}
if($password != $cnfrmPass){
    $error = "Passwords do not match";
}
if($email != $cnfrmEmail){
    $error = "Emails do not match";
}

//if no errors occur start the registration process
if(empty($error) === true){
    $selectQ = dbConnect()->prepare("SELECT `Username` FROM `usersInfo` WHERE `Username`=:username");
    //setting the variables to the query
    $selectQ->bindParam(":username", $username);
    $selectQ->execute(); //execute query

    $count = $selectQ->rowCount(); //see if the above query has found anyone
    if($count > 0){
        $error = "User already exists. Please try again.";   
    }else{
        //if no users found start the insert query
        $insertQ = dbConnect()->prepare("INSERT INTO usersInfo (Username, Password, Email) VALUES (:username, :password, :email)");
        //setting the variables to the query
        $insertQ->bindParam(':username', $username);
        $insertQ->bindParam(':password', $password);
        $insertQ->bindParam(':email', $email);

        if($insertQ->execute()){
            //if the insert query executed successfully redirect them
            //to the login in page.
            header('Location: signin.php');
        }else{
            //otherwise show this error.
            $error = "Query could not execute!";
        }
    }
}