使用 AJAX 和 Jquery 将模态表单内容发送到 PHP 文件时遇到问题


trouble sending modal form contents to PHP file with AJAX and Jquery

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

测试.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); }
      });
    });
  });

添加乐队.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>";
}?>

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

在测试中.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);
        }
       });
     });

您可以在此处阅读有关 preventDefault 函数 https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

在 addBand.php将标签标签信息两边的双引号更改为单引号。在 php 中,双引号/单引号内不能有双引号/单引号。

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

此外,使用控制台准确查看使用"网络"选项卡发布的内容也很有帮助 铬 or 火狐 .

如果它适合您,请将其标记为答案。希望这有帮助。