Ciao a tutti,
ho usato il form ajax dove invia i dati ad un altra pagina rimanendo sulla pagina del form.
Ho preso spunto da questo link .
Il form funziona correttamente.
Ho uato lo stesso metodo su un altra pagina che contiene n form identici con gli id del submit numerati nel seguente modo
1) submit del 1 form ha come id="submit0"
2) submit del 2 form ha come id="submit1"
nella mia funzione java ho aggiunto lo stesso codice sia per il submit1 che pre il submit 2 nel seguente modo:
codice:
$(document).ready(function() {
//if submit button is clicked
$('#submit0').click(function () {
alert("ok");
//Get the data from all the fields;
var utente = $('input[name=utente0]');
alert(utente.val());
//Simple validation to make sure user entered something
//If error found, add hightlight class to the text field
if (utente.val()=='') {
utente.addClass('hightlight');
return false;
} else utente.removeClass('hightlight');
//organize the data properly
var data = utente.val();
var pippo='utente=' + encodeURIComponent(data.replace(/\n\r/g,"~").replace(/\n/g,"~").replace(/\r/g,"~"));
//alert(pippo);
//var testo=comment.val();
//var testo2=testo.replace(/String.fromCharCode(10)gi/, String.fromCharCode(93));
//alert(testo2);
//alert(testo.charCodeAt(3));
//testo.replace(vbCrLf,"-").replace(vbCrLf,"-").replace(/\r/gi,"-").replace(/([^>\%0D\%0A]?)(\%0D\%0A|%0A\%0D|\%0D|%0A)/gi,"-");
//alert(testo.charCodeAt(4));
//organize the data properly
//var data = 'comment=' + testo2;
//var data = 'comment=' + encodeURIComponent(comment.val());
//data.replace(/%0D%0A/gi,"
");
//data.replace(/([^>\%0D\%0A]?)(\%0D\%0A|%0A\%0D|\%0D|%0A)/g, '$1$2');
//alert(data);
//disabled all the text fields
$('.text').attr('disabled','true');
//show the loading sign
$('.loading').show();
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "aggiorna_utenti.aspx",
//GET method is used
type: "POST",
//pass the data
data: pippo,
//Do not cache the page
cache: false,
//success
success: function (html) {
//if process.php returned 1/true (send mail success)
if (html==1) {
//hide the form
$('.loading').fadeOut('slow');
//show the success message
$('.done').fadeIn('slow');
$('#mex').fadeIn('slow');
$('.done').fadeOut('slow');
$('#mex').fadeOut('slow');
//document.getElementById('mex2').innerHTML = comment.val();
//if process.php returned 0/false (send mail failed)
}
else
{ alert('Si è verificato un errore. Ti invitiamo a riprovare più tardi!');
alert(html);
}
}
});
//cancel the submit button default behaviours
return false;
});
//if submit button is clicked
$('#submit1').click(function () {
alert("ok");
//Get the data from all the fields;
var utente = $('input[name=utente1]');
alert(utente.val());
//Simple validation to make sure user entered something
//If error found, add hightlight class to the text field
if (utente.val()=='') {
utente.addClass('hightlight');
return false;
} else utente.removeClass('hightlight');
//organize the data properly
var data = utente.val();
var pippo='utente=' + encodeURIComponent(data.replace(/\n\r/g,"~").replace(/\n/g,"~").replace(/\r/g,"~"));
//alert(pippo);
//var testo=comment.val();
//var testo2=testo.replace(/String.fromCharCode(10)gi/, String.fromCharCode(93));
//alert(testo2);
//alert(testo.charCodeAt(3));
//testo.replace(vbCrLf,"-").replace(vbCrLf,"-").replace(/\r/gi,"-").replace(/([^>\%0D\%0A]?)(\%0D\%0A|%0A\%0D|\%0D|%0A)/gi,"-");
//alert(testo.charCodeAt(4));
//organize the data properly
//var data = 'comment=' + testo2;
//var data = 'comment=' + encodeURIComponent(comment.val());
//data.replace(/%0D%0A/gi,"
");
//data.replace(/([^>\%0D\%0A]?)(\%0D\%0A|%0A\%0D|\%0D|%0A)/g, '$1$2');
//alert(data);
//disabled all the text fields
$('.text').attr('disabled','true');
//show the loading sign
$('.loading').show();
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "aggiorna_utenti.aspx",
//GET method is used
type: "POST",
//pass the data
data: pippo,
//Do not cache the page
cache: false,
//success
success: function (html) {
//if process.php returned 1/true (send mail success)
if (html==1) {
//hide the form
$('.loading').fadeOut('slow');
//show the success message
$('.done').fadeIn('slow');
$('#mex').fadeIn('slow');
$('.done').fadeOut('slow');
$('#mex').fadeOut('slow');
//document.getElementById('mex2').innerHTML = comment.val();
//if process.php returned 0/false (send mail failed)
}
else
{ alert('Si è verificato un errore. Ti invitiamo a riprovare più tardi!');
alert(html);
}
}
});
//cancel the submit button default behaviours
return false;
});
});
Come potete vedere ho ripetuto la funzione per i 2 submit che hanno nome diverso
codice:
$('#submit0').click(function () {
Mi piacerebbe usare solo una funzione magari dentro un ciclo, così se la mia pagina caricherà 20 form, non sono costretto a scrivere 20 funzioni identiche!!!
Grazie!
Ciao
[/CODE]