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

时间:2016-07-06 13:46:24

标签: javascript php jquery ajax twitter-bootstrap

我正在尝试学习如何从bootstrap模式向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>";
}?>

1 个答案:

答案 0 :(得分: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);
        }
       });
     });

您可以在这里了解preventDefault函数https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

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

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

使用控制台查看使用Chrome或Firefox中的“网络”标签准确发布的内容也很有帮助。

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