提交未在表单上提交的按钮

时间:2015-03-25 14:25:06

标签: php html forms

我已经设置了一个注册表单,我的提交按钮不会通过注册信息填充我的数据库。代码snipet显示了表单的开头以及包含在其中的一些字段。

我把这个设置在一个表中,而不是一个表单,它似乎工作但是当我把它改成一个表格时它停止工作。我已经看过这里的一些其他链接,比如这个HTML PHP Contact Form - Submit Button Not Working? Or PHP Issue?但是我似乎无法让它发挥作用。



<div class = "contact">
		<form class="form-horizontal" role="form" method="post" action=" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> ">
		<div class="form-group">
			<label for="name" class="col-sm-2 control-label">Username:</label>
			<div class="col-sm-10">
				<input type="text" class="form-control" id="username" name="username" value="<?php echo $username ?>">
				<span class="error"><?php echo getErrorMessage('username', $error); ?></span>
			</div> 
		</div>
			
		<div class="form-group">
			<label for="password" class="col-sm-2 control-label">Password:</label>
			<div class="col-sm-10">
				<input type="password" class="form-control" id="password" name="password" value="<?php echo $password ?>">
			</div>
		</div>
		
		<div class="form-group">	
			<label for="conPass" class="col-sm-2 control-label">Confirm Password:</label>
			<div class="col-sm-10">
				<input type="password" class="form-control" id="conPassword" name="conPassword" value="<?php echo $conPass ?>">
			</div>

		<div class="col-sm-10">
			<input id="button" name="reg" type="submit" value="Register" class="btn btn-primary">
		</div>	
		</div> <!-- registration button --> 
		
		</form>
	</div> <!-- registration end --> 
&#13;
&#13;
&#13;

编辑:我的表单在我尝试验证后停止向数据库提交信息,对于Stack Overflow非常新的不完整的帖子感到抱歉

<?php
    include "config.php"; 

    //error_reporting(0); 

/*  $username = $_POST['username']; 
    $password = md5($_POST['password']."ALS52KA09W");
    $conPass = md5($_POST['conPass']."ALS52KA09W");
    $telephone = $_POST['telephone'];
    $address1 = $_POST['address1'];
    $town = $_POST['town'];
    $postcode = $_POST['postcode'];
    $forename = $_POST['forename'];
    $surname = $_POST['surname'];
    $email = $_POST['email']; */

    //declaring variables as empty strings
    $username = $forename = $surname = $email = $telephone = $address1 = $town = $postcode =""; 
    $error = array();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["username"])) 
        {
        $error['username'] = "* Username is required";
        } else { 
        $username = test_input($_POST["username"]);
            if (!preg_match("/^[a-zA-Z ]*$/",$username)) 
            {
            $error['username'] = "* Only letters and white space allowed in name";
            }// end if
                  } //end username

        if (empty($_POST["forename"])) 
        {
        $error['forename'] = "* Forename is required";
        } else { 
        $forename = test_input($_POST["forename"]);
            if (!preg_match("/^[a-zA-Z ]*$/",$forename)) 
            {
            $error['forename'] = "* Only letters and white space allowed in forename";
            }// end if
                  } //end forename

        if (empty($_POST["surname"])) 
        {
        $error['surname'] = "* Surname is required";
        } else { 
        $surname = test_input($_POST["surname"]);
            if (!preg_match("/^[a-zA-Z ]*$/",$surname)) 
            {
            $error['surname'] = "* Only letters and white space allowed in forename";
            }// end if
                  } //end surname

        if (empty($_POST["email"])) 
        {
            $error['email'] = "* Email is required";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address syntax is valid
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
            {
                $error['email'] = "* Invalid email format";
            }
               } //end email validation

        if (empty($_POST["telephone"])) 
        {
            $error['telephone'] = "* Telephone is required";
        } else {
                $telephone = test_input($_POST["telephone"]);
                // check telephone only contains numbers and is length 11
            if((preg_match("/[^0-9]/", '', $str)) && strlen($str) == 11)
            {
                $error['telephone'] = "* Please enter a valid telephone number";
            }
                   } //end telephone validation

        /* if (empty($_POST["address1"])) 
        {
            $error['address1'] = "* Address is required";
        } else {
            $address1 = test_input($_POST["address1"]);
            // check if address has letters and numbers
            if (!preg_match("/([A-Za-z0-9]+)/", $address1)) 
            {
                $error['address1'] = "* The address field must contain numbers and letters ";
            }
               } //end address validation */

        if (empty($_POST["town"])) 
        {
            $error['town'] = "* Town is required";
        } else {
                $town = test_input($_POST["town"]);
                // check town only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$town)) 
            {
                $error['town']= "* Only letters and white space allowed";
            }
                } //end town validation

        if (empty($_POST["postcode"])) 
        {
            $error['postcode'] = "Postcode is required";
        } else {
                $postcode = test_input($_POST["postcode"]);
                // check postcode validation and syntax
            if (preg_match ("^[A-Z]?[A-Z][0-9][A-Z0-9]?\s[0-9][A-Z]{2}$^", $postcode))
            {
                $error['postcode'] = "Wrong postcode syntax.  Valid syntax = XX00 0XX";
            }
                } //end postcode validation

    if (!count($error)) {
         $noError = true;
    }
} //end server request method 

    $successMessage = isset($noError) ? 'Thank you for registering with us.' : '';

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    function getErrorMessage($type, $error)
    {
        return isset($error[$type]) ? $error[$type] : '';
    }



    //Registration point
    if(isset($username,$password,$conPass,$forename,$surname,$email,$telephone,$address1,$town,$postcode))
    {
        if($password == $conPass)
        {
            $q = $db->prepare("SELECT * FROM user WHERE username = ?");
            $query = $q-> execute(array($username));
            $count = $q->rowCount();
            if($count == 0)
            {
                $query = $db->prepare("INSERT INTO user SET username = ?, password = ?, forename = ?, surname = ?, email = ?, telephone = ?, address1 = ?,
                                                            town = ?, postcode = ?");
                $query = $query->execute(array($username,$password,$forename,$surname,$email,$telephone,$address1,$town,$postcode));
                    if($query){
                        echo $successMessage;
                        header("Location:home2_template.html");
                        return; 
                    }
            }else{
                echo "This user already exists";
            }

        }else{
            echo "Your passwords do not match";
        } //Error checking
    }


?>

这是填充数据库的总PHP脚本。在注册点注释之下的所有内容都应该在我添加任何内容之前工作。

1 个答案:

答案 0 :(得分:0)

我没有在我的文档顶部声明$ password和$ conPass的变量,而这是阻止提交按钮无法工作的原因。

感谢此帖子上的任何人输入。