【问题标题】:Submit button not submitting on a form提交按钮未在表单上提交
【发布时间】:2023-04-10 01:30:02
【问题描述】:

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

我在表格而不是表格中设置了它,它似乎可以工作,但是当我将其更改为表格时它停止工作。我在这里查看了其他一些链接,例如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 --> 

编辑:我的表单在尝试验证后停止向数据库提交信息,对于 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 脚本。 //Registration point 注释下方的所有内容都应该像我在其上方添加任何内容之前一样正常工作。

【问题讨论】:

  • “它停止工作”。什么停止工作:您的表单停止提交或您的 php 脚本没有看到表单变量。如果是后者,您能否展示您应该处理表单提交的 PHP 代码?
  • I have set up a registration form and my submit button won't pass through to populate my database with the registration information. The code snipet shows the start of my form and some fields that are included in it. --> 然后检查你的数据库入口代码,因为在我的情况下你的代码正在提交。还有一件事,请发布完整的工作,您不能输入 html 语法并询问为什么我的数据库条目不起作用..
  • 我在下面添加了我的 php 代码,并尝试将 method="post" 添加到表单标签,但它仍然不起作用。

标签: php html forms


【解决方案1】:

我没有在我的文档顶部声明 $password 和 $conPass 的变量,这就是阻止提交按钮不起作用的原因。

感谢任何人对此帖子的意见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多