javascript - How to produce a modal on form submit instead of a page redirect -


to give little background, using mailchimp api add email addresses form on site mailchimp email list. found example / snippet here (modified in code) passes emails list, instead of having page redirect once form submitted, want modal pop (i.e. ajax-ify it).

here's i'm starting with:

html:

                     <form role="form" id="signup" class="formee form-inline" action="subscribe.php" method="post">                         <div class="form-group">                             <label class="sr-only" for="email-input">email address                             </label>                             <div class="email-input-wrapper">                                 <input type="text" id="email" name="email" class="form-control input-lg" placeholder="name@school.edu" size="40" value="">                             </div>                         </div>                         <div class="form-group">                            <button class="btn btn-lg" id="request-invite-btn" type="submit" title="send" value="send"> request invite </button>                          </div>                     </form> 

javascript:

        <script type="text/javascript">         $(document).ready(function() {         // jquery validation         $("#signup").validate({             // if valid, post data via ajax             submithandler: function(form) {                 $.post("subscribe.php", { email: $("#email").val() }, function(data) {                     $('#response').html(data);                 });             },             // fields required             rules: {                 email: {                     required: true,                     email: true                 }             }         });     });     </script> 

subscribe.php:

<?php    require_once 'inc/mcapi.class.php'; $api = new mcapi('************************');  // submit subscriber data mailchimp // parameters doc, refer to: http://apidocs.mailchimp.com/api/1.3/listsubscribe.func.php $retval = $api->listsubscribe( '********', $_post["email"], $merge_vars, 'html', false, true );  if ($api->errorcode){     echo "<h4>please try again.</h4>"; } else {     echo "<h4>thank you, have been added our mailing list.</h4>"; } 

?>

modal html (i'm using bootstrap):

     <div class="modal fade" id="emailmodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true">       <div class="modal-dialog">         <div class="modal-content">           <div class="modal-header">             <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">&times;</span></button>             <h4 class="modal-title" id="mymodallabel">success!</h4>           </div>           <div class="modal-body">             <p>              interest!             </p>           </div>           <div class="modal-footer">             <button type="button" class="btn btn-primary" data-dismiss="modal">ok</button>           </div>         </div>       </div>     </div> 

i used mcapi.class.php doc site @ top.

i'm new this, apologize posting unnecessary code. direction appreciated. thanks!

if want display modal when server respond can following :

doc : http://getbootstrap.com/javascript/#via-javascript

<script type="text/javascript">         $(document).ready(function() {         // handle submission event         $("#signup").submit(function(event){             event.preventdefault();         });         // jquery validation         $("#signup").validate({             // if valid, post data via ajax             submithandler: function(form) {                 $.post("subscribe.php", { email: $("#email").val() }, function(data) {                     //$('#response').html(data);                     $('#emailmodal').modal();                                         });             },             // fields required             rules: {                 email: {                     required: true,                     email: true                 }             }         });     }); </script> 

this code trigger modal on server response example, feel free improve needs :)

apparently, post deal redirecting , validate jquery plugin stackoverflow post.

so can bind event on form submission , prevent default.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -