javascript - How do I submit a poMMo mailing list form using Ajax without refresh? -
javascript - How do I submit a poMMo mailing list form using Ajax without refresh? -
i have searched on here ways this, simple ajax submission forms have been asked about.
if not familiar pommo, mailing list management software allows developers implement custom forms onto websites sole purpose of collecting emails mailing lists. possible merge ajax , pommo forms together?
the code have been using is:
test.php
<form action='_test.php' method='post' class='ajaxform'> <input type='text' name='txt' value='enter e-mail address'> <input type='submit' value='submit'> </form> </head> <div id='testdiv'></div>
_test.php
<?php $arr = array( 'testdiv' => $_post['txt'] ); echo json_encode( $arr ); ?>
jsfile.js
jquery(document).ready(function(){ jquery('.ajaxform').submit( function() { $.ajax({ url : $(this).attr('action'), type : $(this).attr('method'), datatype: 'json', info : $(this).serialize(), success : function( info ) { for(var id in data) { jquery('#' + id).html( data[id] ); } } }); homecoming false; }); });
i checking reply questions , provide info willing help. please , give thanks you. :)
here code created:
<form action="" name="signup"> <fieldset> <legend>subscribe</legend> <div> <label for="email"><strong>your email:</strong></label> <input type="text" name="email" id="email" maxlength="60" /> </div> </fieldset> <div id="buttons"> <input type="hidden" id="pommo_signup" name="pommo_signup" value="true" /> <input type="submit" class="button" value="subscribe" /> </div> </form> // javascript document $(document).ready(function() { $('.button').click(function() { var email = $("input#email").val(); var pommo_signup = $("input#pommo_signup").val(); var info = 'email='+ email + '&pommo_signup='+ pommo_signup; $.ajax({ type: 'post', url: 'http://localhost/pommo/user/process.php', data: data, datatype: 'json', success: function(json){ if (!json.success){ alert(json.errors); } else { $("form").html("<div id='message'></div>"); $('#message').html('<h2>subscription received!') .append("<p>we in touch soon.</p>") .hide() .fadein(1500); } }, error: function(json){ } }); homecoming false; }); });
you need add together pommo process.php file in order json info request:
/********************************** json output initialization *********************************/ pommo::requireonce($pommo->_basedir.'inc/classes/json.php'); $json = new pommojson();
i set main error messages in validation part of process.php calling fail method in json.php file located in /inc/classes/json.php:
/********************************** validate input *********************************/ if (empty ($_post['pommo_signup'])) pommo::redirect('login.php'); $subscriber = array( 'email' => $_post['email'], 'registered' => time(), 'ip' => $_server['remote_addr'], 'status' => 1, 'data' => @$_post['d'], ); // ** check right email syntax if (!pommohelper::isemail($subscriber['email'])){ $json->fail(pommo::_t('invalid e-mail address. please seek again.')); } // ** check if email exists in db ("duplicates bad..") if (pommohelper::isdupe($subscriber['email'])) { $json->fail(pommo::_t('error email exists in database.')); }
finally, set success message after email sent user in add together subscriber section of process.php:
if subscription requires confirmation:
// send confirmation message. if (pommohelpermessages::sendmessage(array('to' => $subscriber['email'], 'code' => $subscriber['pending_code'], 'type' => 'confirm'))) { $subscriber['registered'] = date("f j, y, g:i a",$subscriber['registered']); if ($comments || isset($notices['pending']) && $notices['pending'] == 'on') pommohelpermessages::notify($notices, $subscriber, 'pending', $comments); $json->success(pommo::_t('subscription request received.')); if ($config['site_confirm']) pommo::redirect($config['site_confirm']); }
and here here if doesn't...
else { // send/print welcome message pommohelpermessages::sendmessage(array('to' => $subscriber['email'], 'type' => 'subscribe')); $subscriber['registered'] = date("f j, y, g:i a",$subscriber['registered']); if ($comments || isset($notices['subscribe']) && $notices['subscribe'] == 'on') pommohelpermessages::notify($notices, $subscriber, 'subscribe',$comments); $json->success(pommo::_t('subscription request received.')); // redirect if ($config['site_success']) pommo::redirect($config['site_success']); }
i ended not using success message but, need run success method turn true instead of false, otherwise json.success never false.
let me know if helps!
javascript forms jquery form-submit
Comments
Post a Comment