http://mydomain.org/newsletter/?p=subscribe&id=1&VerificationCodeX=&email=jimmybaloney%40hellodolly.com&htmlemail=1&list%5B1%5D=signup&listname%5B1%5D=members&makeconfirmed=1&subscribe=Subscribe
<input type="submit" name="accion" id="accion" value="Submit" />
<input type="hidden" name="subscribe" value="it's done" />
?p=subscribe&id=1
function frmValidate()
{
//declarations
var x; var y; var strErrors; var strURL;
//the HTML DIV where the errors will show up.
document.getElementById('subscribebox_errors').innerHTML="";
//function call....returns how many checkboxes have actually been checked.
var x = numberChecked();
//function call....uses regular expressions to validate if the value in the email form box is
//indeed a valid email. Please note, I had to add an actual ID tag to the form, not just use
//the HTML name tag.
var y = validateEmail(document.getElementById('ml_email').value);
//if neither a checkbox is checked, or it's not a valid email.
if( (!(x>=1)) || (!y) )
{
//build the error text/html string
strErrors='<ul class="error_text">Errors found:';
if(!(x>=1))
{
strErrors = strErrors + '<li>Please select at least one newsletter.</li>';
}
if(!y)
{
strErrors = strErrors + '<li>Please input a valid e-mail address.</li>';
}
strErrors = strErrors + '</ul>';
//write the error HTML to the appropriate error area.
document.getElementById("subscribebox_errors").innerHTML=" " + strErrors;
}
else if( (x>=1) && (y=1) )
{
//its all good! Begin the AJAX post procedure.
ajaxSendSubscribeInfo();
}
else
{
//if you get here, something went really wrong.
alert('Error!');
}
}
function ajaxSendSubscribeInfo()
{
//declarations
var urlStr; var urlParams; var returnResponse;
var sForm;
var xmlhttp;
//standard AJAX object creation
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//place your PHPList URL here. IMPORTANT! Be sure to add ?p=subscribe and the &id= if you are using these functions.
urlStr = 'http://www.mydomain.org/lists/?p=subscribe&id=2';
//declare your AJAX form
sForm = document.forms["subscribeform"];
//prepping POST post string. Should probably use a proper URL encoding. Haven't got there yet.
urlParams = urlParams + '&email=' + escape(sForm.elements["email"].value);
//iterate through all the checkboxes. IMPORTANT side note regarding PHPLIST. The list names in your form must be in this format.
// list[1]=signup list[2]=signup etc. Be sure to use the word "signup" (all lower case, no spaces) as the value for each list, or
// phplist won't recognize it.
for(var i=0;i<sForm.elements.length;i++)
{
if( (sForm.elements[i].type=="checkbox") && (sForm.elements[i].checked) )
{
urlParams=urlParams + '&' + escape(sForm.elements[i].name) + '=' + escape(sForm.elements[i].value);
}
}
//must add the subscribe variable to the POST parameters.
urlParams = urlParams + '&subscribe=hitme';
//Start POSTING via AJAX.
xmlhttp.open("POST",urlStr,true);
//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", urlParams.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4)
{
if (xmlhttp.status == 200)
{ // only if "OK"
var returnResponse = String(xmlhttp.responseText);
document.getElementById('subscribebox').innerHTML="Thank you for subscribing. Please check your email for information about confirming your subscription.";
}
else
{
document.getElementById('subscribebox').innerHTML="An Error has occured while submitting your request. Please contact the webmaster.";
}
}
}
xmlhttp.send(urlParams);
}
function validateEmail(elementValue)
{
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(elementValue);
}
function numberChecked()
{
var numChecked = 0;
var sForm = document.forms["subscribeform"];
for(var i=0;i<sForm.elements.length;i++)
{
if( (sForm.elements[i].type=="checkbox") && (sForm.elements[i].checked) )
{
numChecked++;
}
}
return numChecked;
}
<form action="" method="" id="newsletter">
<input type="hidden" name="VerificationCodeX" value="" />
<input type="hidden" name="list[2]" value="signup">
<input type="hidden" name="listname[2]" value="Name_of_List"/>
<label for="name">Name</label> <input type="text" size="20" value="" name="attribute1" id="name" /><br />
<label for="email">E-mail</label> <input type="text" size="20" value="" name="email" id="email" />
<a href="javascript:;" onclick="$.ajax({type: 'POST', data: $('#newsletter').serialize(), url: 'lists/?p=subscribe&id=2', success: function(msg){ alert('Thank you for your registration. Please check your email to confirm.'); }})">OK</a>
</form>
<form method="post" name="newsletter" id="newsletter" action="/lists/?p=subscribe&id=2">
<input type="text" name="email" value="" size="40" />
<input type="hidden" name="htmlemail" value="1" />
<input type="hidden" name="list[2]" value="signup" />
<input type="hidden" name="listname[2]" value="XXXX" />
<input type="hidden" name="VerificationCodeX" value="" />
<input type="submit" name="subscribe" id="go" value="Subscribe to the Selected Newsletters" />
</form>
<span id="eee"></span>
<script type="text/javascript">
$(document).ready(function () {
$('#go').click(function () {
$.ajax({
type: 'POST',
data: $('#newsletter').serialize(),
url: $('#newsletter').attr('action'),
success: function (msg) {
$('#eee').html(msg);
alert('Thank you for your registration. Please check your email to confirm.');
}
})
return false;
});
});
</script>
borsodas wrote:after comparing headers between my ajax page that did not work and the standard phplist submit form i found that the name value pair <input type=submit name="subscribe" value="Subscribe" onClick="return checkform();"> were not going through. so i added them into a hidden input and it now works. possibly because of "return false" in the ajax call. but it works now!
Return to Advanced Questions & Problems
Users browsing this forum: No registered users and 0 guests