【问题标题】:Post modal form values to a PHP file将模态表单值发布到 PHP 文件
【发布时间】:2017-09-25 14:58:04
【问题描述】:

我有一个模态表单。当我按下提交按钮时,php 文件运行时出现以下错误: 注意:未定义索引:第 2 行 C:\xampp\htdocs\site\bank.php 中的 phone1 注意:未定义索引:第 4 行 C:\xampp\htdocs\site\bank.php 中的charge1

这是bank.php中的PHP代码:

<?php
echo  $_POST["phone"];
echo "</br>";
echo  $_POST["charge"];
?>

这里是模态内容:

<!-- Modal content-->

<div class="modal fade" id="register" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal title here</h4>
      </div>
      <div class="modal-body">
        <form id="modal-form" class="form-horizontal" method="POST" action="bank.php">
          <div class="form-group col-md-12">
            <div class="form-group">
              <div class="col-xs-6" style="float:right;">
                <label>phone numer:</label>
                <input id="phone" type="text" class="form-control" name="phone" value="pre filled value" disabled/>
                <span id='display'></span>
              </div>
              <div class="col-xs-6" style="float:left;">
                <label>charge:</label>
                <input style="font-size:17px; font-weight:bold;" id="charge" type="text" class="form-control" name="charge" value="some other prefilled value" disabled/>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-xs-12">
              <button class="btn btn-danger btn-default pull-right" data- dismiss="modal" style="margin-left:10px;">close</button>
              <button type="submit" class="btn btn-success"> submit </button>
            </div>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        modal footer
      </div>
    </div>

  </div>
</div>

如果有人帮我找出问题所在,我将不胜感激

【问题讨论】:

  • 您的表单有phone1,而您的回显是$_POST["phone"],哪个是正确的?
  • 好吧,名字是phone1而不是phone....而charge1不是charge....发送的是名字,而不是ids....
  • 您使用数字(phone1,charge1)命名参数,但您的 php 代码在没有数字的情况下回显它们($_POST['change']
  • 表单发布名称属性,而不是 id。因此,无论您在 name 属性中添加什么,都将显示在 PHP 端。
  • 表单本身不会发送禁用的参数。您的问题的可能答案here

标签: javascript php jquery html twitter-bootstrap


【解决方案1】:

不再相关,OP 编辑​​了他的问题。 查看相关答案的编辑

post 变量的名称由输入的 nameattribute 定义,而不是 id。 因此,在您的 bank.php 中,您将拥有 $_POST['phone1']$_POST['charge1']available。 您可以随时 var_dump($_POST); 查看所有可用的帖子变量。

编辑:此外,禁用的输入不会在提交时发送。您可以通过为每个禁用的输入添加第二个隐藏输入来解决这个问题:

<!-- Modal content-->

<div class="modal fade" id="register" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal title here</h4>
      </div>
      <div class="modal-body">
        <form id="modal-form" class="form-horizontal" method="POST" action="bank.php">
          <div class="form-group col-md-12">
            <div class="form-group">
              <div class="col-xs-6" style="float:right;">
                <label>phone numer:</label>
                <input id="phoneDisabled" type="text" class="form-control" name="phoneDisabled" value="pre filled value" disabled/>
                <input id="phone" type="hidden" class="form-control" name="phone" value="pre filled value" disabled/>
                <span id='display'></span>
              </div>
              <div class="col-xs-6" style="float:left;">
                <label>charge:</label>
                <input style="font-size:17px; font-weight:bold;" id="chargeDisabled" type="text" class="form-control" name="chargeDisabled" value="some other prefilled value" disabled/>
                <input style="font-size:17px; font-weight:bold;" id="charge" type="hidden" class="form-control" name="charge" value="some other prefilled value" disabled/>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-xs-12">
              <button class="btn btn-danger btn-default pull-right" data- dismiss="modal" style="margin-left:10px;">close</button>
              <button type="submit" class="btn btn-success"> submit </button>
            </div>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        modal footer
      </div>
    </div>

  </div>
</div>

Edit2:或者,只需将disabled属性更改为readonly

【讨论】:

  • 我已将 name1 更改为 name 但仍然是同样的问题,这是 var_dump($_POST) 结果:array(0) { }
  • 哇,非常感谢,禁用字段是重点,非常感谢
【解决方案2】:

$_POST 变量中的名称和 html 中的 input 名称都不同。请尝试使它们相同,并希望您的代码能够正常工作。

name=phone1 更改为name=phone 或其他任何内容。

【讨论】:

    【解决方案3】:

    您已将这两个输入都设置为禁用。默认情况下,表单不会发布这些。您可能需要使用 JavaScript 获取变量,或者再次使用 JavaScript 删除禁用的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2011-01-16
      相关资源
      最近更新 更多