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

Matt Molsberry

I'm trying to learn how to submit form data to a PHP file from a bootstrap modal. From the other questions I've seen, I thought I had it right, but I keep getting the error dialog. I must be missing something obvious.

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>";
}?>
Isabel Inc

I looked through your code and found quite a few errors.

In your test.php change the Submit button from a button to an actual submit button.

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

In your Jquery Add the preventDefault() function to stop the form from submitting to the same page.

     $("#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);
        }
       });
     });

You can read about what the preventDefault function here https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

In your addBand.php change the double quotes around your label label-info to single quotes. You cannot have double/single quotes inside double/single quotes inside php.

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

Also it helps to use the console to see exactly what is being posted using the Network tab in Chrome or Firefox .

Please mark this as the answer if it works for you. Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sending a dynamic form via jquery ajax php

From Dev

Modal form Submission to PHP using Jquery .ajax

From Dev

PHP Ajax form sending

From Dev

PHP Ajax form sending

From Dev

Sending contact form data using Ajax/jQuery/PHP

From Dev

Ajax sending data from other php file contact form

From Dev

AJAX not sending Form Data to PHP

From Dev

AJAX not sending Form Data to PHP

From Dev

Jquery dialog form not posting field contents correctly to PHP file

From Dev

Error in Sending Form file with form using AJAX

From Dev

JQuery Ajax not sending data to PHP

From Dev

ajax jquery form stopped sending emails

From Dev

Sending data combined in a form as an array in AJAX/JQuery

From Dev

JQuery AJAX - Sending a JSONP form with attached files

From Dev

Sending a form via Ajax vs PHP

From Dev

having trouble sending facebook notification via ajax call to php

From Dev

Trouble sending attr int variable through ajax, to php, to db column

From Dev

php form showing php file and not sending it on

From Dev

AJAX responseXML empty with jQuery UI Modal Form

From Dev

Trouble w/ Form Processing using jQuery + PHP

From Dev

Trouble w/ Form Processing using jQuery + PHP

From Dev

Form not submitting file / Undefined index: FileInput - PHP/AJAX/jQuery

From Dev

Modal form made in Bootstrap with JavaScript and PHP not sending email on submit

From Dev

jQuery sending file with ajax to MVC Controller

From Dev

jQuery sending file with ajax to MVC Controller

From Dev

sending variables to php via ajax and jquery

From Dev

Sending JSON via Jquery AJAX call to PHP

From Dev

Ajax sending empty array to php file

From Dev

Ajax is sending data but Php file is not recieving it

Related Related

HotTag

Archive