ciao a tutti!
è il primo script che tento di fare con l'ausilio di ajax e quindi chiedo il vostro aiuto!
allora:
ho una pagina con un form, chiamiamola pagina.php, dove l'action è $_SERVER['PHP_SELF'] e method POST, la quale richiama lo script per l'invio che si chiama sender.php (incude_once("ls/sender.php")
.
Nella pagina.php ho un div vuoto (<div id="dati"></div>) nel quale vorrei far visualizzare progressivamente il risultato di ogni invio mail (da come avete capito è un invio multiplo di email).
Ho preso questo pezzo di codice googleggiando per l'oggetto xmlhttprequest (so che non è completo ma mi serve per ora solo a fare test)
Codice PHP:
function createObject() {
var tipo_richiesta;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer") {
tipo_richiesta = new ActiveXObject("Microsoft.XMLHTTP");
} else {
tipo_richiesta = new XMLHttpRequest();
}
return tipo_richiesta;
}
var http = createObject();
function inviadati(valore) {
//image progress
var par = window.parent.document;
var images = par.getElementById('dati');
var new_div = par.createElement('div');
var new_img = par.createElement('img');
new_img.src = 'img/indicator.gif';
new_div.appendChild(new_img);
images.appendChild(new_div);
http.open('get','ls/sender.php?testo=' + valore + '&oggetto=' + oggetto + '&email=' + email);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4) {
var response = http.responseText;
document.getElementById('dati').innerHTML = response;
}
}
questo esempio invia i datia sender.php tramite GET... invece come devo procedere per utilizzare method POST anziche GET?
Questo è un pezzo di codice esempio di sender.php utilizzando PhpMailer (ps= alcune parti sono commentate perchè questo script è richiamato da un'altra pagina php che già utilizza quelle istruzioni):
Codice PHP:
//error_reporting(E_ALL);
//set_time_limit(0);
//ob_start("ob_gzhandler");
require("libs/phpmailerv2/class.phpmailer.php");
$vars = getConf();
if(isset($_POST['initsend'])) {
$mailmittente = $vars[0];
$nomemittente = $vars[1];
$serversmtp = $vars[2];
$tiposervizio = $vars[3];
$titolo = $_POST['titolomail'];
$html = $_POST['codice'];
$alt = $_POST['alttext'];
$embeddedimages = rtrim($_POST['embeddedimages'],',');
$arrayembedded = explode(",",$embeddedimages);
$allegati = $_POST['allegatichecked'];
$destinatari = $_POST['destinatari'];
$mail = new PHPMailer();
$mail->From = $mailmittente;
$mail->FromName = $nomemittente;
$mail->Host = $serversmtp;
$mail->Mailer = $tiposervizio;
$arraydest = explode(",",$destinatari);
$arrayall = explode(",",$allegati);
foreach($arraydest as $email) {
$mail->Subject = "TEST PROVA";
// HTML body
$body = $html;
foreach($arrayembedded as $emb) {
$ext = explode(".",$emb);
if($ext[1]=="jpg" || $ext[1]=="jpe" || $ext[1]=="jpeg") {
$mail->AddEmbeddedImage("allegati/".$emb, $emb, $emb, "base64", "image/jpg");
} elseif($ext[1]=="png") {
$mail->AddEmbeddedImage("allegati/".$emb, $emb, $emb, "base64", "image/png");
} elseif($ext[1]=="bmp") {
$mail->AddEmbeddedImage("allegati/".$emb, $emb, $emb, "base64", "image/bmp");
} elseif($ext[1]=="gif") {
$mail->AddEmbeddedImage("allegati/".$emb, $emb, $emb, "base64", "image/gif");
} else {
$mail->AddEmbeddedImage("allegati/".$emb, $emb, $emb, "base64", "application/octet-stream");
}
}
foreach($arrayall as $allegato) {
$mail->AddAttachment('./allegati/'.$allegato);
}
//$mail->IsHTML(true);
//$mail->Sender = "PHP5";
// Plain text body (for mail clients that cannot read HTML)
$text_body = $alt;
$mail->Body = $body;
$mail->AltBody = $text_body;
$mail->AddAddress($email);
if(!$mail->Send()) {
echo "There has been a mail error sending to " . $email . " ".$mail->ErrorInfo."
";
} else {
echo "Email ok a ".$email."
";
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}
}
//ob_end_flush();
Spero di essere stato chiaroe che mi possiate dare una mano!
grazie
edit:
in questo esempio trovato su gooooogle l'invio dei dati è inizializzato così:
Codice PHP:
<input type="button" onclick="javascript:inviadati(document.theform.word.value,document.theform.ecc ecc);" value="invia" />