单击时突然禁用提交按钮

时间:2016-02-10 07:22:17

标签: javascript php jquery twitter-bootstrap mysqli

我想在数据库中插入简单的用户名。 我在bootstrap中编写了代码,然后将其复制到netbeans,然后在其中插入php代码。 脚本运行良好,但是当我尝试提交以在数据库中插入名称时,提交按钮被禁用,并且没有给出任何错误或重定向到另一个页面。 请帮忙。 我是bootstrap的新手。

<?php 

    include 'includes/db_connection.php';
    if (isset($_POST['submit']))
 {

   //$name = filter_input(INPUT_POST, 'name',FILTER_SANITIZE_STRING);
   //$firstname=filter_string('firstname');
   $firstname = $_POST['firstname'];
   $sql1 ="INSERT INTO student (firstname)
VALUES ('$firstname')";

$res1 = mysqli_query($conn, $sql1);  
if($res1)
{
    //$last_id = mysqli_insert_id($conn);
    header("Location: successful_message.php");
    //echo 'Successful';
   // . " Last inserted ID is: " . $last_id;
}
else 
{

    header("Location: valid_test.php");
}

}
else{?>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
   <link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link  type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" type="text/ecmascript"></script>

<script src="http://formvalidation.io/vendor/formvalidation/js/formValidation.min.js"></script>
<link rel="stylesheet" href="http://formvalidation.io/vendor/formvalidation/css/formValidation.min.css">
<script src="http://formvalidation.io/vendor/formvalidation/js/framework/bootstrap.min.js"></script>

<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">


    <title>Form</title>

    <!-- Bootstrap core CSS -->

    <!-- Custom styles for this template -->
  </head>
<body>


    <form id="contactForm" method="post" action="validate-input.php" class="form-horizontal">
   <div class="container-fluid">
        <div class="container">
    <div class="form-group">
        <label class="col-xs-3 control-label">Full name</label>


        <div class="col-xs-4">
            <input type="text" class="form-control" name="firstname" placeholder="First name" />
        </div>


    <div class="form-group">
        <div class="col-xs-9 col-xs-offset-3">
            <button type="submit" value="submit" name="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
</form>
  <?php } ?>

<script>
$(document).ready(function() {
    // Generate a simple captcha
    function randomNumber(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    function generateCaptcha() {
        $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));
    }

    generateCaptcha();

    $('#contactForm')
        .formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                firstname: {
                    row: '.col-xs-4',
                    validators: {
                        notEmpty: {
                            message: 'The first name is required'
                        },
                        regexp: {
                            message: 'Name only contains Letter',
                            regexp: /^[A-Z a-z]*$/
                        }

                    }
                },
                lastName: {
                    row: '.col-xs-4',
                    validators: {
                        notEmpty: {
                            message: 'The last name is required'
                        }
                    }
                },
                phoneNumber: {
                    validators: {
                        notEmpty: {
                            message: 'The phone number is required'
                        },
                        regexp: {
                            message: 'The phone number can only contain the digits, spaces, -, (, ), + and .',
                            regexp: /^[0-9\s\-()+\.]+$/
                        }
                    }
                },
                email: {
                    validators: {
                        notEmpty: {
                            message: 'The email address is required'
                        },
                        emailAddress: {
                            message: 'The input is not a valid email address'
                        }
                    }
                },
                message: {
                    validators: {
                        notEmpty: {
                            message: 'The message is required'
                        },
                        stringLength: {
                            max: 700,
                            message: 'The message must be less than 700 characters long'
                        }
                    }
                },
                captcha: {
                    validators: {
                        callback: {
                            message: 'Wrong answer',
                            callback: function(value, validator, $field) {
                                var items = $('#captchaOperation').html().split(' '),
                                    sum   = parseInt(items[0]) + parseInt(items[2]);
                                return value == sum;
                            }
                        }
                    }
                }
            }
        })
        .on('err.form.fv', function(e) {
            // Regenerate the captcha
            generateCaptcha();
        });
});
</script>

        </div><!--End container -->
   </div><!-- End container-fluid -->




</body>
</html>

2 个答案:

答案 0 :(得分:1)

  1. 从表单中删除操作
  2.   

    <form id="contactForm" method="post" class="form-horizontal">

    1. 您正在对脚本进行大量验证。删除firstname以外的其他验证 我尝试了但仍需要两次,所以提交后,再次更改名字然后再次提交。不知道是不是因为你的formvalidation.js的代码

      <?php
      //include 'includes/db_connection.php';
      if (isset($_POST['submit'])) {
      $firstname = $_POST['firstname'];
      $sql1 = "INSERT INTO student (firstname) VALUES ('$firstname')";
      
      $res1 = mysqli_query($conn, $sql1);
      if ($res1) {
      echo 'succ';
      //    header("Location: successful_message.php");
      } else {
      echo 'fail';
      //    header("Location: valid_test.php");
      }
      } else {
      ?>
      

                                                                     

        <script src="http://formvalidation.io/vendor/formvalidation/js/formValidation.min.js"></script>
        <link rel="stylesheet" href="http://formvalidation.io/vendor/formvalidation/css/formValidation.min.css">
        <script src="http://formvalidation.io/vendor/formvalidation/js/framework/bootstrap.min.js"></script>
      
        <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
      
      
        <title>Form</title>
      </head>
      <body>
        <form id="contactForm" method="post" class="form-horizontal">
          <div class="form-group">
            <label class="col-xs-3 control-label">Full name</label>
            <div class="col-xs-4">
              <input type="text" class="form-control" name="firstname" placeholder="First name" />
            </div>
          </div>
          <div class="form-group">
            <label class="col-xs-3 control-label">Pass</label>
            <div class="col-xs-4">
              <input type="text" class="form-control" name="password" placeholder="First name" />
            </div>
          </div>
      
          <div class="form-group">
            <div class="col-xs-9 col-xs-offset-3">
              <button type="submit" value="submit" name="submit" class="btn btn-primary">Submit</button>
            </div>
          </div>
        </form>
      <?php } ?>
      
      <script>
        $(document).ready(function () {
          // Generate a simple captcha
          function randomNumber(min, max) {
            return Math.floor(Math.random() * (max - min + 1) + min);
          }
      
          function generateCaptcha() {
            $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));
          }
      
          generateCaptcha();
          $('#contactForm')
                  .formValidation({
                    framework: 'bootstrap',
                    icon: {
                      valid: 'glyphicon glyphicon-ok',
                      invalid: 'glyphicon glyphicon-remove',
                      validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                      firstname: {
                        validators: {
                          notEmpty: {
                            message: 'The first name is required'
                          },
                          regexp: {
                            message: 'Name only contains Letter',
                            regexp: /^[A-Z a-z]*$/
                          }
                        }
                      },
                      password: {
                        validators: {
                          notEmpty: {
                            message: 'The password is required'
                          }
                        }
                      }
                    }
                  });
          ;
        });
      </script>
      

答案 1 :(得分:0)

您正在检查

 if (isset($_POST['submit']))
 {

而不是action="validate-input.php"action=""

  

将操作留空,以便将表单提交给   文件的地址,即同一页。