2016-02-10 48 views
0

我試圖在數據庫中插入簡單的用戶名。 我在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> 
+0

哪個頁面是validate-input.php ?? –

+0

我寫這段代碼的主要頁面 –

+0

你可以發佈驗證輸入代碼: –

回答

0

上面要檢查

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

代替action="validate-input.php"action=""

假動作空,這樣的表格將被提交給 文件的地址,即同一頁面。從表單

+0

第二點沒有任何區別..你是否把它或你不.. – Rayon

+0

@YO喲 仍然沒有發送價值分貝 –

+0

@RayonDabre 你建議做什麼? –

1
  1. 刪除操作

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

  • 你確認要多少你的腳本。刪除除名字以外的其他驗證 我試過但仍需要兩次,所以在提交後,再次更改名字,然後再次提交。不知道是不是因爲你的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> 
    

  • +0

    哎! 值正在提交,但爲什麼驗證不起作用? 我想提交它與自舉設計和驗證 –

    +0

    Thanx好友。 其實問題出在這一行 .. –