duandoucou7200 2015-03-25 14:25
浏览 64
已采纳

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

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.

I had this set up in a table, rather than a form and it seemed to work but when I changed it to a form it stoped working. I've looked at a few other links on here such as this one HTML PHP Contact Form - Submit Button Not Working? Or PHP Issue? But I can't seem to get it working.

<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 --> 

EDIT: my form stopped submitting information to the database after I tried to validate it, sorry for the incomplete posts very new to 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
    }


?>

This is my total PHP script to populate the database. Everything below the //Registration point comment should work as it was working before I added anything above it.

</div>
  • 写回答

1条回答 默认 最新

  • dqitk20644 2015-03-26 14:33
    关注

    I didn't declare the variables of $password and $conPass at the top of my document and that's what was stopping the submit button not to work.

    Thanks for anyone's input on this post.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况