2014-09-01 204 views
-1

此表單提交不工作,誰能幫我...形式不提交/工作

我使用的引導CSS和試圖提交一份簡單的表格,有一個小問題,但我可以「T檢測到問題,請大家幫忙

<div class="container"> 
    <div class="thumbs askus container col-md-6 col-md-offset-4"> 
     <form id="logfrm" action="" method="post" class="form-horizontal"> 
      <div class="form-group"> 
      <label for="inputEmail3" class="col-sm-2 control-label">Username</label> 
      <div class="col-sm-4"> 
       <input type="text" name="unm" class="form-control" id="inputUser" placeholder="Username"> 
      </div> 
      </div> 
      <div class="form-group"> 
      <label for="inputPassword3" class="col-sm-2 control-label">Password</label> 
      <div class="col-sm-4"> 
       <input type="password" name="pwd" class="form-control" id="inputPassword" placeholder="Password"> 
      </div> 
      </div> 
      <div class="form-group"> 
      <div class="col-sm-offset-2 col-sm-4"> 
       <div class="checkbox"> 
       <label> 
        <input type="checkbox"> Remember me 
       </label> 
       </div> 
      </div> 
      </div> 
      <div class="form-group"> 
      <div class="col-sm-offset-2 col-sm-4"> 
       <button class="btn btn-default" >Sign in</button> 
      </div> 
      </div> 
     </form> 
    </div> 
</div> 

PHP代碼的形式處理

if (isset($_POST['unm'])) { 
if ($_POST['unm']=="admin" && $_POST['pwd']=="xxxxxx") { 
    echo '<script type="text/javascript">alert("Hi Admin");</script>'; 
    header('Location: home.php'); 
} 
else{ 
    echo '<script type="text/javascript">alert("Unknown Username/Bad password");</script>'; 
} 

}

+0

那麼什麼不起作用? – Daan 2014-09-01 10:32:12

+1

表單標籤中的「操作」字段爲空。把那裏的PHP腳本地址。 – ojovirtual 2014-09-01 10:35:26

+0

action =「」同一頁? – 2014-09-01 10:35:30

回答

1

你只是錯過了動作URL,這樣表單提交不工作......我剛剛從動作變更表單動作標籤=「」行動=「action.php的」其中action.php是包含您的PHP代碼來處理HTML表單的文件。使用下面的代碼,這將解決您的問題。

home.php

<?php 
    if(isset($_POST['submit_button'])) 
    { 

if ($_POST['unm']=="admin" && $_POST['pwd']=="xxxxxx") { 
    echo '<script type="text/javascript">alert("Ok");</script>'; 
    header('Location:home.php'); 
} 
else{ 
    echo '<script type="text/javascript">alert("Unknown Username/Bad password"); if(confirm("Unknown Username/Bad password")){document.location.reload(true);}</script>'; 

} 
} 

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"> 
    <title>Bootstrap 101 Template</title> 

    <!-- Bootstrap --> 
    <!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> 

<!-- Optional theme --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"> 

<!-- Latest compiled and minified JavaScript --> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> 
</head> 
<body><br/><br/><br/><br/> 
    <div class="container"> 
    <div class="thumbs askus container col-md-6 col-md-offset-4"> 
     <form id="logfrm" method="post" class="form-horizontal" action="home.php"> 
      <div class="form-group"> 
      <label for="inputEmail3" class="col-sm-2 control-label">Username</label> 
      <div class="col-sm-4"> 
       <input type="text" name="unm" class="form-control" id="inputUser" placeholder="Username"> 
      </div> 
      </div> 
      <div class="form-group"> 
      <label for="inputPassword3" class="col-sm-2 control-label">Password</label> 
      <div class="col-sm-4"> 
       <input type="password" name="pwd" class="form-control" id="inputPassword" placeholder="Password"> 
      </div> 
      </div> 
      <div class="form-group"> 
      <div class="col-sm-offset-2 col-sm-4"> 
       <div class="checkbox"> 
       <label> 
        <input type="checkbox"> Remember me 
       </label> 
       </div> 
      </div> 
      </div> 
      <div class="form-group"> 
      <div class="col-sm-offset-2 col-sm-4"> 
       <button type="submit" class="btn btn-default" name="submit_button" >Sign in</button> 
      </div> 
      </div> 
     </form> 
    </div> 
</div> 
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.min.js"></script> 
    </body> 
    <?php } ?> 
</html> 
+0

我正在提交d表格到同一頁 – sarath 2014-09-01 11:52:25

+0

@SarathMohan我改變了我的代碼,因爲你需要... – 2014-09-03 04:37:59

+0

問題已解決... bt我沒有得到d實際麻煩造成的原因,無論如何它被解決了 – sarath 2014-09-03 05:36:52

1

如果你的這個表單的php在同一個頁面上,那麼將這個操作指向同一頁面是有意義的。這樣,表單將被髮送到頁面的刷新版本,PHP將從該頁面中檢索並處理它。

所以 - 如果你形成一個稱爲form.php的

然後頁面上應該是

<form id="logfrm" action="form.php" method="post" class="form-horizontal"> 

您可以使用Firebug看到這一切都是監控$ _ POST [「UMN」]發送正確

+0

我已經嘗試過,頁面沒有導航到其他頁面提交 – sarath 2014-09-01 11:20:15

+0

在頁面上的PHP的順序是重要的 - 當表單重新提交到同一頁面時,它將運行從頁面頂部的腳本下來,所以它可能是在頁面上定位的PHP的問題 - 在同一頁上的PHP在哪裏? – Mobaz 2014-09-02 22:44:05