Bootstrap模态表单提交错误

时间:2017-07-27 06:30:38

标签: php forms twitter-bootstrap

我知道这个问题在SO中有很多解决方案。但我仍然无法发现我的错误。以下是我的自助模式形式

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="assets/lib/bootstrap/css/bootstrap.min.css">
  <script src="../asset/modal/js/jquery.min.js"></script>
  <script src="assets/lib/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<? if(isset($_POST["sendmail"]))
   echo "Form 1 have been submitted";
    ?>
<div class="container">

  <!-- Trigger the modal with a button -->

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    <? echo $token;  ?>
      <!-- Modal content-->
      <form class="form-horizontal" role="form" method="post" name="sendmail" >
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <textarea rows="8" class="form-control"></textarea>
        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-default" >Send</button>
        </div>
      </div>
      </form>
    </div>
  </div>

</div>

</body>
</html>

但每当我提交表单时,它都会重定向到我项目的索引页面。请帮助我发现我的错误。: - (

2 个答案:

答案 0 :(得分:2)

是的,因为你没有把它指向任何地方......你需要写下这样的东西: -

   <form class="form-horizontal" role="form" method="post" name="sendmail" action="page_name_here.php" >

因此您需要添加action="page_name_here.php">

如果您想留在同一页面上,请写下action="#">

更新: -

 <button type="submit" class="btn btn-default" name="send">Send</button>

name添加到按钮send,然后添加到Php中: -

<? if(isset($_POST["send"]))
 echo "Form 1 have been submitted";
 ?>

这样,当您点击send按钮时,表单中的所有内容都会提交,因为它的按钮为type=submit

答案 1 :(得分:0)

将其用于提交按钮。对于POST,您的按钮尚未被命名为sendmail,因此信息将无法通过。

<? if(isset($_SERVER['REQUEST_METHOD'] == 'POST' ))
   echo "Form 1 have been submitted";
?> 

添加空行动,如同同一页所示

 <form class="form-horizontal" role="form" method="post" name="sendmail" action="">