Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  • Thread Tools
  • Rate This Thread
  1. #1
    New to the CF scene
    Join Date
    May 2016
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Modal not showing after AJAX call

    I have a form that emails the web admin using a PHP file but I'm trying to use an AJAX call to keep the same page up and display a modal but the page just refreshes itself and no email is sent.
    Here is the form:

    Code:
    <form accept-charset="UTF-8" id="mcaForm" action="" method="post">
                  <p style="text-align: center;"><input class="span3" name="name" id="name" placeholder="Name" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="title" id="title" placeholder="Title" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="company" id="company" placeholder="Company" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="address" id="address" placeholder="Address" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="email" id="email" placeholder="Email" type="text" required>
    							<p style="text-align: center;"><input type="checkbox" name="agree" required value="accept">By checking this box you agree to the Mutual Confidentiality Agreement>
                 <div><button class="btn btn-inverse btn-large" type="submit">Submit</button></div>
    						</form>
    		<div id="mcamodal" class="modal fade" role="dialog">
      		<div class="modal-dialog">
        		<div class="modal-content">
          	  <div class="modal-header">
                <h4 class="modal-title">Modal Header</h4>
              </div>
               <div class="modal-body">
    				    <img src=assets/img/modallogo.png>
                <p>Some text in the modal.</p>
              </div>
          	    <div class="modal-footer">
                </div>
            </div>
          </div>
        </div>
    And here is my AJAX:

    Code:
    <script type="text/javascript">
    
    $( "#mcaForm" ).submit(function( event ) {
     event.preventDefault();
    
     $.ajax({
       url: 'acknowledge.php',
       type: 'POST',
       data:  $('#mcaForm').serialize(),
       success: function(response) {
       if(response == 'Success') {
           $('#mcamodal').html("Success");
           $('#mcamodal').modal('show');
       },
    });
    });
    </script>

  2. #2
    New to the CF scene
    Join Date
    May 2016
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    bad copy/paste job but i cant edit so i apologize for the structure

  3. #3
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,123
    Thanks
    30
    Thanked 250 Times in 248 Posts
    Quote Originally Posted by Gowat View Post
    I have a form that emails the web admin using a PHP file but I'm trying to use an AJAX call to keep the same page up and display a modal but the page just refreshes itself and no email is sent.
    Here is the form:


    Code:
    <script type="text/javascript">
    
    $( "#mcaForm" ).submit(function( event ) {
     event.preventDefault();
    
     $.ajax({
       url: 'acknowledge.php',
       type: 'POST',
       data:  $('#mcaForm').serialize(),
       success: function(response) {
       if(response == 'Success') {//this is the success function it is successful
    response will be returned data so unless you return "Success"  the following 
    will not run ...
           $('#mcamodal').html("Success");
           $('#mcamodal').modal('show');
       },// comma does not belong
    });
    });
    </script>
    Welcome to the Forum
    Last edited by DaveyErwin; 05-05-2016 at 08:35 PM.
    Cleverness is serviceable for
    everything,
    sufficient in nothing.

  4. Users who have thanked DaveyErwin for this post:

    Gowat (05-05-2016)

  5. #4
    New to the CF scene
    Join Date
    May 2016
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you for the response! Im not sure what you mean by returning 'Success' would you mind elaborating further?

  6. #5
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,123
    Thanks
    30
    Thanked 250 Times in 248 Posts
    success: function(response) {

    response === whatever the
    url returns

    this is the success function
    no need to test for success
    Last edited by DaveyErwin; 05-05-2016 at 09:44 PM.
    Cleverness is serviceable for
    everything,
    sufficient in nothing.

  7. Users who have thanked DaveyErwin for this post:

    Gowat (05-05-2016)

  8. #6
    New to the CF scene
    Join Date
    May 2016
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Right so I have success being echoed in the php

    Code:
    <?php
    if (isset($_POST['formmSubmit'])) {
         echo "success";
         $to = '@gmail.com';
         $subject = 'MCA Submitted';
         $message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
         $message .= 'Email: ' . $_POST['email'] . "\r\n\r\n";
         $message .= 'Title: ' . $_POST['title'] . "\r\n\r\n";
         $message .= 'Address: ' . $_POST['address'] . "\r\n\r\n";
         $message .= 'Company: ' . $_POST['company'];
         $headers = "From: @gmail.com\r\n";
         $headers .= 'Content-Type: text/plain; charset=utf-8';
         $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
            if ($email) {
         $headers .= "\r\nReply-To: $email";
         }
         $success = mail($to, $subject, $message, $headers);
         $response = array();
         $response['success'] = $success;
         $response['general_message'] = $message;
         $response['errors']  = $errors;

  9. #7
    New to the CF scene
    Join Date
    May 2016
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Wait im not sure i get what you mean so i shouldnt have if(response == 'Success')?

  10. #8
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,123
    Thanks
    30
    Thanked 250 Times in 248 Posts
    Code:
    <?php
    if(isset($_POST['name'])){
     echo $_POST["name"];
    } else {
     ?>
     <html>
     <head>
     <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
      <title>BMQ Evaluation & Consulting - Image Viewer - Unternehmen</title> 
    </head>
     <body>
    <form accept-charset="UTF-8" id="mcaForm" action="" method="post">
                  <p style="text-align: center;"><input class="span3" name="name" id="name" placeholder="Name" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="title" id="title" placeholder="Title" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="company" id="company" placeholder="Company" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="address" id="address" placeholder="Address" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="email" id="email" placeholder="Email" type="text" required>
           <p style="text-align: center;"><input type="checkbox" name="agree" required value="accept">By checking this box you agree to the Mutual Confidentiality Agreement>
                 <div><button class="btn btn-inverse btn-large" type="submit">Submit</button></div>
          </form>
      <div id="mcamodal" class="modal fade" role="dialog">
        <div class="modal-dialog">
          <div class="modal-content">
             <div class="modal-header">
                <h4 class="modal-title">Modal Header</h4>
              </div>
               <div class="modal-body">
            <img src=assets/img/modallogo.png>
                <p>Some text in the modal.</p>
              </div>
               <div class="modal-footer">
                </div>
            </div>
          </div>
        </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    
    
    <script type="text/javascript">
    
    
    $( "#mcaForm" ).submit(function( event ) {
      event.preventDefault();alert('SucKess')
      $.ajax({
         url: 'acknowledge.php',
         type: 'POST',
         data:  $('#mcaForm').serialize(),
         success: function(response) {
          
               alert(response)
          
      }
      });
    });
    </script>
     </body>
     </html> 
    <?php } ?>
    Cleverness is serviceable for
    everything,
    sufficient in nothing.

  11. Users who have thanked DaveyErwin for this post:

    Gowat (05-05-2016)

  12. #9
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,123
    Thanks
    30
    Thanked 250 Times in 248 Posts
    Quote Originally Posted by Gowat View Post
    Right so I have success being echoed in the php

    Code:
    <?php
    if (isset($_POST['formmSubmit'])) {
         echo "success";
         $to = '@gmail.com';
         $subject = 'MCA Submitted';
         $message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
         $message .= 'Email: ' . $_POST['email'] . "\r\n\r\n";
         $message .= 'Title: ' . $_POST['title'] . "\r\n\r\n";
         $message .= 'Address: ' . $_POST['address'] . "\r\n\r\n";
         $message .= 'Company: ' . $_POST['company'];
         $headers = "From: @gmail.com\r\n";
         $headers .= 'Content-Type: text/plain; charset=utf-8';
         $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
            if ($email) {
         $headers .= "\r\nReply-To: $email";
         }
         $success = mail($to, $subject, $message, $headers);
         $response = array();
         $response['success'] = $success;
         $response['general_message'] = $message;
         $response['errors']  = $errors;
    capital S
    Cleverness is serviceable for
    everything,
    sufficient in nothing.

  13. Users who have thanked DaveyErwin for this post:

    Gowat (05-05-2016)

  14. #10
    Regular Coder
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    365
    Thanks
    1
    Thanked 50 Times in 47 Posts
    1) If the form is a scripting only response, why does it have markup? Shouldn't it be built on the DOM?

    2) Where's your fieldset? What makes a bunch of input tags be grammatical paragraphs? Why do you have static style in the markup? Why are you using a button to do an input's job? Much less...

    WHY are you telling users to go **** themselves by using placeholder to do label's job?!?

    Placeholder Attribute Is Not A Label! | Web Axe
    https://www.nngroup.com/articles/for...-placeholders/
    https://www.paciellogroup.com/blog/2...der-attribute/

    Even the bloody specification:
    HTML 5.1

    ... and I quote:
    "The placeholder attribute should not be used as a replacement for a label."

    Much less I verily doubt that semantically you're actually down to a H4, since what H3 is that a subsection of?!? (probably a <legend>'s job, but since you have no fieldsets...)

    When the markup is that malformed, it's hardly a shock that the giant middle finger to speed and sane code that is jQuery (or at least it looks like jQuery) probably doesn't work worth a damned either.

    Inaccessible form, that's always present planned for zero graceful degradation, with the fat bloatd train-wreck of developer ineptitude that is jQuery... Toss it and start over.
    From time to time the accessibility of websites must be refreshed with the blood of designers and owners; it is its natural manure.
    http://www.cutcodedown.com

  15. #11
    New to the CF scene
    Join Date
    May 2016
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I'm not at liberty to change the design they wanted placeholders in the field not labels above them. I obviously haven't been doing this as long or as frequently as you so why instead of getting all worked up you could just as easily educate me tactfully.

  16. #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,131
    Thanks
    3
    Thanked 814 Times in 803 Posts
    Did you fix the capital S on success?
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  17. #13
    New to the CF scene
    Join Date
    May 2016
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I actually got rid of
    Code:
    if(response == 'Success')
    and that made it so the modal popped up after i click submit but theres still no email functionality.
    Here is my updated code:
    Code:
    <form accept-charset="UTF-8" id="mcaForm" action="" method="post">
                  <p style="text-align: center;"><input class="span3" name="name" id="name" placeholder="Name" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="title" id="title" placeholder="Title" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="company" id="company" placeholder="Company" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="address" id="address" placeholder="Address" type="text" required>
                  <p style="text-align: center;"><input class="span3" name="email" id="email" placeholder="Email" type="text" required>
    							<p style="text-align: center;"><input type="checkbox" name="agree" required value="accept">By checking this box you agree to the Mutual Confidentiality Agreement>
                 <div><button class="btn btn-inverse btn-large" id="send" name="send" type="submit">Submit</button></div>
    						</form>
    		<div id="mcamodal" class="modal fade" role="dialog">
      		<div class="modal-dialog">
        		<div class="modal-content">
               <div class="modal-body">
    				    <img src=assets/img/modallogo.png>
                <p>Some text in the modal.</p>
              </div>
            </div>
          </div>
        </div>
    
    <script type="text/javascript">
    
    $( "#mcaForm" ).submit(function( event ) {
     event.preventDefault();
    
     $.ajax({
       url: 'acknowledge.php',
       type: 'POST',
       data:  $('#mcaForm').serialize(),
    	 success: function(response) {
           $('#mcamodal').modal('show');
       }
    });
    });
    </script>
    and here is the new php:
    Code:
    <?php
    if (isset($_POST['send'])) {
       
       $to = '@gmail.com';
       $subject = 'MCA Submitted';
       $message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
       $message .= 'Email: ' . $_POST['email'] . "\r\n\r\n";
       $message .= 'Title: ' . $_POST['title'] . "\r\n\r\n";
       $message .= 'Address: ' . $_POST['address'] . "\r\n\r\n";
       $message .= 'Company: ' . $_POST['company'];
       $headers = "From: @gmail.com\r\n";
       $headers .= 'Content-Type: text/plain; charset=utf-8';
       $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
       if ($email) {
       $headers .= "\r\nReply-To: $email";
    }
       $success = mail($to, $subject, $message, $headers);
    }
    ?>
    
    <body>
      <?php if (isset($success) && $success) { ?>
      Your message has been sent.
      <?php } else { ?>
      <h1>Oops!</h1>
      Sorry, there was a problem sending your message.
      <?php } ?>
    </body>
    If i change the form action to acknowledge.php the email works just fine but I'm thinking my php file is missing something...


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •