Hi!
Here is a possible solution. Its not tested.. 🙂
The file 'sendNewsletter.php':
$mail = trim($_GET['mail']);
// Chech for legal format.
if(!myIsMail($mail)) die();
// Check if this address realy should have a newsletter.
if(!myIsInDatabase($mail)) die();
/*
Run your mailsending stuff.
*/
if($mailWasSendt)
echo 'SUCCESS';
The file with javascript:
<?php
// newsletter.php
?>
<html>
<head>
<title>newsletter sender</title>
<script language="javascript">
<?php
// Connect database ang get the mailaddresses.
$mailaddresse = myDbGetAddresses();
// Start to make a string for a javascript array.
$tmpStr = '[';
foreach($mailaddresse as $address)
$tmpStr.= "'$address',";
// Removes last ','
$tmpStr = rtrim($tmpStr, ',');
$tmpStr.= ']';
// Print the addresses as a javascript array.
echo "var addresses = $tmpStr;\n";
?>
//----------------------------------------------------------
function getRequestObject()
{
if(window.XMLHttpRequest) {
try {requestObj = new window.XMLHttpRequest;}
catch(e) {requestObj = false;}
}
else if(window.ActiveXObject) {
axObj = ['Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP'];
axObjCount = axObj.length;
for(var i = 0; i < axObjCount; i++) {
try {requestObj = new ActiveXObject(axObj[i]);}
catch(e) {requestObj = false;}
if(typeof requestObj == 'object')
break;
}
}
else
requestObj = false;
return requestObj;
}
//----------------------------------------------------------
var scriptAddress = 'http://wwww.myDomain.com/sendNewsletter.php';
var mailCounter = 0;
var requestObj = false;
function sendMail()
{
// If all are sendt.
if(mailCounter > addresses.length - 1)
return;
requestObj = getRequestObject();
if(requestObj === false) {
alert('Sorry but you need to change browser.');
return;
}
adddress = addresses[mailCounter];
url = scriptAddress + '?mail=' + adddress;
requestObj.onreadystatechange = stateChange;
requestObj.open("GET", url, true);
requestObj.send("");
}
//----------------------------------------------------------
function stateChange()
{
if(requestObj.readyState == 4) {
if(requestObj.status == 200) {
respons = requestObj.responseText;
// Trim the string
respons = respons.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
if(respons == 'SUCCESS') {
div = document.getElementById('listLayer');
div.innerHTML = div.innerHTML + '<br>' + addresses[mailCounter];
mailCounter++;
}
else
alert('There was some problems...');
}
}
}
//----------------------------------------------------------
</script>
</head>
<body>
Her is your form.<br>
<form action="" method="post" onsubmit="sendMail();return false;">
</form>
<div id="listLayer">
</div>
<body>
</html>