【问题标题】:trouble sending modal form contents to PHP file with AJAX and Jquery使用 AJAX 和 Jquery 将模态表单内容发送到 PHP 文件时遇到问题
【发布时间】:2016-07-06 13:46:24
【问题描述】:

我正在尝试学习如何从引导模式将表单数据提交到 PHP 文件。从我看到的其他问题来看,我认为我做对了,但我不断收到错误对话框。我一定遗漏了一些明显的东西。

test.php

<html>  
   <body>
  <div class="container padding-top-10 change-width">    
    <div class="row padding-top-20" align="center">
      <button class="btn btn-warning btn-lg" data-toggle="modal" data-target="#bandModal">Add Band(s)</button>
    </div>
  </div>



  <div id="thanks"></div>
  <div class="modal fade" id="bandModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
      <div class="modal-content modal-lg">
        <!-- Modal Header -->
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
          </button>
          <h4 class="modal-title" id="bandModalLabel">
                    Add a Show
                </h4>
        </div>
        <!-- Modal Body -->
        <div class="modal-body row">

          <div class="container col-md-12">
            <form id="addBandForm">
              <h3>Band Details<small>Enter each band name and primary contact information...</small></h3>
              <div class="well" id="newBandRows">
                <div class="row">
                  <div class="col-md-3">
                    <div class="form-group">
                      <label for "newBandName">Band Name:</label>
                      <input type="text" class="form-control" id="newBandName" name="newBandName" placeholder="Enter Band Name" />
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label for="primaryContact">Primary Contact:</label>
                      <input type="text" class="form-control" id="primaryContact" name="primaryContact" placeholder="Enter Name" />
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label for "personEmail">Primary Email:</label>
                      <input type="email" class="form-control" id="primaryEmail" name="primaryEmail" placeholder="Enter Email" />
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label for "personPhone">Primary Phone #:</label>
                      <input type="text" class="form-control" id="primaryPhone" name="primaryPhone" placeholder="Enter Phone #" />
                    </div>
                  </div>
                </div>
              </div>
              <div id="newRowButton">
                <div class="row">
                  <div class="col-md-1">
                    <button type="button" class="btn btn-success pull-left" onClick="addNewBandRow();">+</button>
                  </div>
                  <div id="remover" class="col-md-1">

                  </div>
                  <div class="col-md-7">
                  </div>
                  <div class="col-md-3 padding-top-10">
                    <button id="addBandSubmit" class="btn btn-primary pull-right">Submit</button>
                  </div>
                </div>
              </div>
              <script src="js/newBand.js" type="text/javascript"></script>
            </form>
          </div>

        </div>

        <div class="modal-footer">
        </div>

      </div>
    </div>
  </div>
  </body>

  </html>

jQuery

             $(function() {
    //twitter bootstrap script
    $("#addBandSubmit").click(function() {
      $.ajax({
        type: "POST",
        url: "womhScripts/addBand.php",
        data: $('#addBandForm').serialize(),
        success: function(msg) {
          $("#thanks").html(msg)
          $("#bandModal").modal('hide');
        },
        error: function(xhr, status, error) { var err = eval("(" + xhr.responseText + ")"); alert(err.Message); }
      });
    });
  });

addBand.php

    <?php
if (isset($_POST['newBandName'])) {
$bandName = strip_tags($_POST['newBandName']);
$contact = strip_tags($_POST['primaryContact']);
$email = strip_tags($_POST['primaryEmail']);
$phone = strip_tags($_POST['primaryPhone']);

echo "bandName      =".$bandName."</br>"; 
echo "contact       =".$contact."</br>"; 
echo "email     =".$email."</br>"; 
echo "phone     =".$phone."</br>"; 
echo "<span class="label label-info" >your message has been submitted .. Thanks you</span>";
}?>

【问题讨论】:

  • 看看你的开发者控制台,看看里面有没有东西
  • 另外看看这些$contact - $primaryContact 和那里的其他变量。错误报告应该向您抛出未定义的变量通知,并因此而默默地失败。
  • 您遇到什么错误?从你所说的我理解 ajax 请求返回一个错误。你在正确的路径中发布吗???你可以尝试错误:function(xhr, status, error) { var err = eval("(" + xhr.responseText + " )");警报(错误消息); } 并告诉我们实际的错误是什么。
  • 我在这个问题上徘徊够久了。按分钟/小时向我付款,如果你愿意,我会在这里呆一整天。你一直是cmets;阅读并理解它们。我出去了。
  • 您需要养成accepting answers 的习惯,它可以帮助您解决问题。您将获得积分,并鼓励其他人帮助您。

标签: javascript php jquery ajax twitter-bootstrap


【解决方案1】:

我查看了您的代码,发现了很多错误。

在您的 test.php 中,将提交按钮从一个按钮更改为一个实际的提交按钮。

  <input id="addBandSubmit" type="submit" class="btn btn-primary pull-right">Submit</input>

在你的 Jquery 中 添加 preventDefault() 函数以阻止表单提交到同一页面。

     $("#addBandForm").submit(function(event) {
      event.preventDefault();
       $.ajax({
         url: "womhScripts/addBand.php",
         type: "POST",
         data: $('#addBandForm').serialize(),
         success: function(msg) {
           $("#thanks").html(msg);
           $("#bandModal").modal('hide');
         },
         error:function(errMsg) {
           console.log(errMsg);
        }
       });
     });

您可以在https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault 阅读有关 preventDefault 功能的信息

在您的 addBand.php 中,将标签 label-info 周围的双引号更改为单引号。 php 内的双引号/单引号内不能有双引号/单引号。

echo "<span class='label label-info' >your message has been submitted .. Thanks you</span>";

还有助于使用控制台查看使用 Chrome 或 Firefox 中的网络选项卡发布的确切内容。

如果对您有用,请将其标记为答案。希望这会有所帮助。

【讨论】:

  • 非常感谢!我不知道 chrome 中的控制台......再次,谢谢。另外,我没有设置按钮类型,因为我不知道阻止默认方法......你有什么建议我可以阅读以了解我应该知道的其他常用方法吗?跨度>
  • 这取决于你想要实现什么。您可以随时查看 Stack 或 Github 上的代码。有大量的开源库具有用于各种目的的出色代码。编码愉快。
猜你喜欢
  • 2023-02-11
  • 2011-04-19
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
相关资源
最近更新 更多