无法在bootstrap模式中显示PHP $ _POST变量

时间:2015-02-11 12:56:22

标签: php twitter-bootstrap

我想在使用post方法提交表单后,在bootstrap模式中显示一个数字。

但是bootstrap模式无法显示数字($ _POST [number])。

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label class="control-label" >Number</label>
<div class="controls">
    <input class="form-control" name="number" type="text">

    <p class="help-block">Enter a number</p>
</div>
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" type="submit">submit</button>
<?php
$number = $_POST['number'];
?>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close"
                        data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    number
                </h4>
            </div>
            <div class="modal-body">
                <?php
                echo $number;
                ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">close
                </button>
                <button type="button" class="btn btn-primary">
                    ok
                </button>
            </div>
        </div>
    </div>
</div>

我该怎么做?

6 个答案:

答案 0 :(得分:1)

您的方法有一些观察和缺陷:

  • 修复HTML错误(例如,您未正确关闭表单,请添加:</form>
  • PHP是一种服务器端脚本语言。
  • 如果没有Jquery(客户端脚本语言),Bootstrap就无法运行。
  • 除非Ajax驱动,否则您无法在表单提交按钮上触发Bootstrap模式!
  • 在提交表单(页面加载)see jsfiddle example
  • 后显示模态

完整代码:

<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
  </head>
  <body>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
      <label class="control-label" >Number</label>
      <div class="controls">
        <input class="form-control" name="number" type="text">        
        <p class="help-block">Enter a number</p>
      </div>
      <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" type="submit">submit</button>
    </form>  
    <?php if (isset($_POST["number"])) : ?>
    <!-- Show the Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
    aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close"
            data-dismiss="modal" aria-hidden="true">
              &times;
            </button>
            <h4 class="modal-title" id="myModalLabel">
              number
            </h4>
          </div>
          <div class="modal-body">
            <?php
              echo htmlspecialchars($_POST["number"]);
            ?>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default"
            data-dismiss="modal">close
            </button>
            <button type="button" class="btn btn-primary">
              ok
            </button>
          </div>
        </div>
      </div>
    </div>
  </body>  
</html>
<script type="text/javascript">
  $(window).load(function(){
    $('#myModal').modal('show');
  });
</script>
<?php else : ?>
<!-- Do other stuff here -->
<?php endif; ?>

答案 1 :(得分:0)

首先,您没有关闭form代码,而是尝试使用submit按钮作为modal的按钮...我不会&#39不知道那是否会奏效。

关闭您的form代码并添加input类型的submit字段...然后为模态添加button即可。

以下是工作代码:

<html>
<head>
    <link rel="stylesheet" href="../includes/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="../includes/css/bootstrap.min.css">
    </head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
    <label class="control-label" >Number</label>
    <div class="controls">
        <input class="form-control" name="number" type="text">

        <p class="help-block">Enter a number</p>
    </div>
    <input type="submit" value="submit">
    </form>
    <?php
    $number = $_POST['number'];
    ?>
    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" type="submit">Modal</button>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close"
                            data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        number
                    </h4>
                </div>
                <div class="modal-body">
                    <?php
                    echo $number;
                    ?>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default"
                            data-dismiss="modal">close
                    </button>
                    <button type="button" class="btn btn-primary">
                        ok
                    </button>
                </div>
            </div>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="../includes/js/bootstrap.min.js"></script>
</body>
</html>

根据您的需要更改<script><link>代码来源。

答案 2 :(得分:0)

您需要再添加一个按钮(输入提交按钮)。该按钮会将发布请求发送给self并更新变量$ number。

答案 3 :(得分:0)

enter image description here

你可以删除类模型并从“myModel”中删除&#39; class div或者可以为display:block编写样式。我尝试了你的代码并检查了我的浏览器并删除了上面的类。我希望你能解决问题。您还可以通过查看页面来源查看您的号码。

答案 4 :(得分:0)

我也注意到有关此模态问题的一些内容,请从 Sub RowOffset() Application.ScreenUpdating = False 'Worksheets("Mine").Activate Dim rng As Range Dim rng2 As Range Dim i As Long Set rng = ThisWorkbook.ActiveSheet.Range("C87:C37") Set rng2 = Range("C85:N85") With rng For i = .Rows.Count To 1 Step -1 If .Item(i) = "" Then rng2.Select rng2.Delete Shift:=xlUp Set rng2 = rng2.Offset(-1, 0) 'THIS LINE HAS AN ERROR End If Next i End With Application.ScreenUpdating = True End Sub 中移除type="button"并运行代码。

答案 5 :(得分:-1)

会将此作为评论发布,但我还没有足够的代表。

这是因为PHP代码在服务器端执行并在加载HTML之前执行,因此$number是一个空字符串。