Вот код, который я создал:
<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 data = '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 will be in touch soon.</p>")
.hide()
.fadeIn(1500);
}
},
error: function(json){
}
});
return false;
});
});
Вам также нужно добавить это в файл PoMMo process.php, чтобы получить данные JSON из запроса:
/**********************************
JSON OUTPUT INITIALIZATION
*********************************/
Pommo::requireOnce($pommo->_baseDir.'inc/classes/json.php');
$json = new PommoJSON();
Я помещаю свои основные сообщения об ошибках в часть проверки process.php, вызывая метод fail в файле json.php, который находится в /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 for correct email syntax
if (!PommoHelper::isEmail($subscriber['email'])){
$json->fail(Pommo::_T('Invalid e-mail address. Please try again.'));
}
// ** check if email already exists in DB ("duplicates are bad..")
if (PommoHelper::isDupe($subscriber['email'])) {
$json->fail(Pommo::_T('Error the email already exists in the database.'));
}
Наконец, я помещаю свое сообщение об успехе после того, как письмо было отправлено пользователю в разделе добавления подписчика process.php:
Если для подписки требуется подтверждение:
// 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']);
}
и здесь такжездесь, если это не так ...
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']);
}
В итоге я не использовал сообщение об успехе, но вам нужно запустить метод успеха, чтобы установить его в true вместо false, иначе json.success никогда не будетбудь что угодно, кроме лжи.
Дай мне знать, если это поможет!