表单不能在php验证后提交

时间:2016-04-07 11:05:38

标签: php forms

我有一个表单,我想通过PHP验证,我已经把以下代码,但问题是表单没有提交重定向。 我想验证然后提交表单。这是代码

<?php
include'connect.php';
require_once './config.php';
// define variables and set to empty values
$nameErr = $emailErr = $lnameErr = $fnameErr = $passErr = "";
$name = $email = $lname = $fname = $pass = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["username"])) {
        $nameErr = "Username is required";
    } 

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } 
    else {
        $email = ($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email address"; 
        }
    }


    if (empty($_POST["password"])) {
        $passErr = "Password is required";
    }    

    if (empty($_POST["fname"])) {
        $fnameErr = "First Name is required";
    } 
    if (empty($_POST["lname"])) {
        $lnameErr = "Last Name is required";
    } 
}
else{
    if (isset($_POST["sub"])) {
        $fname = trim($_POST["fname"]);
        $lname = trim($_POST["lname"]);
        $name = trim($_POST["username"]);
        $pass = trim($_POST["password"]);
        $email = trim($_POST["email"]);
        $sql = "SELECT COUNT(*) AS count from users where email = :email_id or username = :username_id ";
        try {
            $stmt = $DB->prepare($sql);
            $stmt->bindValue(":email_id", $email);
            $stmt->bindValue(":username_id", $name);
            $stmt->execute();
            $result = $stmt->fetchAll();

            if ($result[0]["count"] > 0) {
                echo"<div>E-mail or Username Already Registered</div>";
            } else {
                $sql = "INSERT INTO `users` (`username`, `password`, `email`, `firstname`, `lastname`) VALUES " . "( :name, :pass, :email, :fname, :lname)";
                $stmt = $DB->prepare($sql);
                $stmt->bindValue(":fname", $fname);
                $stmt->bindValue(":lname", $lname);
                $stmt->bindValue(":name", $name);
                $stmt->bindValue(":pass", md5($pass));
                $stmt->bindValue(":email", $email);
                $stmt->execute();
                $result = $stmt->rowCount();


                if ($result > 0) {
                    require_once "PHPMailerAutoload.php";       
                    $lastID = $DB->lastInsertId();

                    $message = '<html><head>
      <title>Message</title>
      </head>
      <body>';
                    $message .= '<p>Hello!</p>';
                    $message .= "</body></html>";


                    // php mailer code
                    $mail = new PHPMailer(true);
                    $mail->IsSMTP(); // telling the class to use SMTP
                    $mail->SMTPDebug = 0;                     // enables SMTP debug information (for testing)
                    $mail->SMTPAuth = true;                  // enable SMTP authentication
                    $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
                    $mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                    $mail->Port = 587;                   // set the SMTP port for the GMAIL server
                    $mail->Username = 'example@example.com';
                    $mail->Password = 'Password';
                    $mail->SetFrom('example@example.com', 'Name');
                    $mail->AddAddress($email);
                    $mail->Subject = trim("Verification");
                    $mail->MsgHTML($message);

                    try {
                        $mail->send();
                        header('Location: welcome.php');
                    } 
                    catch (Exception $ex) {
                        $msg = $ex->getMessage();
                        $msgType = "warning";
                    }
                } else {
                    $msg = "Failed to create User";
                    $msgType = "warning";
                }
            }
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }
}
?>

0 个答案:

没有答案