Forum Moderators: open
function objetoAjax(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function enviarDatosSolicitud(){
var divResultado = document.getElementById('resultado');
divResultado.style.display = "block";
var enviarajax = document.solicitud.enviarajax.value,
idrand = document.solicitud.idrand.value,
half_round = document.solicitud.half_round.value,
paynow = document.solicitud.paynow.value,
balance = document.solicitud.balance.value,
whenpayment = document.solicitud.whenpayment.value,
arrive = document.solicitud.arrive.value,
depart = document.solicitud.depart.value,
llegada = document.solicitud.llegada.value,
salida = document.solicitud.salida.value,
dias = document.solicitud.dias.value;
ajax = objetoAjax();
ajax.open("POST", "ajax.php", true);
ajax.onreadystatechange = function () {
alert('NO FUNCIONAAAAAAAAAAAAAAAAAAA');
if (ajax.readyState == 4) {
if (ajax.responseText === "Loading, please wait") {
window.location = 'continue.php';
} else {
divResultado.innerHTML = ajax.responseText
<!-- LimpiarCampos();-->
}
}
}
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("dias=" + dias + "&enviarajax=" + enviarajax + "&salida=" + salida +
"&llegada=" + llegada + "&depart=" + depart + "&arrive=" + arrive +
"&idrand=" + idrand + "&half_round=" + half_round + "&paynow=" + paynow + "&balance=" + balance + "&whenpayment=" + whenpayment + "");
}
<form class="instant" name="solicitud" id="solicitud" action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post" onSubmit="return enviarDatosSolicitud();">
<span style ="font-size:12px">Discount coupon code:</span><input type="text" name="idrand" size="10"> </p>
<input type="hidden" name="propiedad" value="<?php print $propiedad;?>">
<input type="hidden" name="llegada" value="<?php print $llegada;?>">
<input type="hidden" name="salida" value="<?php print $salida;?>">
<input type="hidden" name="dias" value="<?php print $dias;?>">
<input type="hidden" name="half_round" value="<?php print $half_round;?>">
<input type="hidden" name="paynow" value="<?php print $paynow;?>">
<input type="hidden" name="balance" value="<?php print $balance;?>">
<input type="hidden" name="whenpayment" value="<?php print $whenpayment;?>">
<input type="hidden" name="arrive" value="<?php print $arrival_display;?>">
<input type="hidden" name="depart" value="<?php print $departure_display;?>">
<p>
<input class="boton roundedcorner border" type="submit" value= "Proceed with booking using PayPal secure system" name="enviarajax" alt="Instant payment"> </p>
<script type="text/javascript" src="ajaxcupones.js"></script>
<div id="resultado"></div>
</form>
function objetoAjax(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function enviarDatoscupon(){
divresult = document.getElementById('result');
enviarajax = document.solicitud.enviarajax.value;
idrand = document.solicitud.idrand.value;
half_round = document.solicitud.half_round.value;
paynow = document.solicitud.paynow.value;
balance = document.solicitud.balance.value;
whenpayment = document.solicitud.whenpayment.value;
arrive = document.solicitud.arrive.value;
depart = document.solicitud.depart.value;
llegada = document.solicitud.llegada.value;
salida = document.solicitud.salida.value;
dias = document.solicitud.dias.value;
ajax = objetoAjax();
ajax.open("POST", "ajax.php", true);
ajax.onreadystatechange = function () {
alert('NO FUNCIONAAAAAAAAAAAAAAAAAAA');
if (ajax.readyState == 4) {
if (ajax.responseText === "Loading, please wait") {
window.location = 'continue.php';
} else {
divresult.innerHTML = ajax.responseText
<!-- LimpiarCampos();-->
}
}
}
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("dias=" + dias + "&enviarajax=" + enviarajax + "&salida=" + salida +
"&llegada=" + llegada + "&depart=" + depart + "&arrive=" + arrive +
"&idrand=" + idrand + "&half_round=" + half_round + "&paynow=" + paynow + "&balance=" + balance + "&whenpayment=" + whenpayment + "");
} the enviarDatosSolicitud function must have a return statement in it for this to work properly. To prevent the default form action (submitting the form, causing a new document to be loaded), you must "return false;" within your function. As you've posted it above, there is no return method at all in that function, so the form will still submit. And by the time the AJAX response comes back, the document that sent the request has now been replaced, and hence you never see the onreadystatechange function called.
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("dias=" + dias + "&enviarajax=" + enviarajax + "&salida=" + salida +
"&llegada=" + llegada + "&depart=" + depart + "&arrive=" + arrive +
"&idrand=" + idrand + "&half_round=" + half_round + "&paynow=" + paynow + "&balance=" + balance + "&whenpayment=" + whenpayment + "");
return false;
}
Is it ok to have several ajax scripts, or should be all in one as per w3c?
If you have more than one AJAX task on your website, you should create ONE standard function for creating the XMLHttpRequest object, and call this for each AJAX task.
// AJAX request #1
var ajax1 = objectoAjax();
ajax1.open("POST", "page1.php", true);
ajax1.onreadystatechange = function () {
// Do some stuff
};
ajax1.send("foo=bar");
// AJAX request #2
var ajax2 = objectoAjax();
ajax2.open("POST", "page2.php", true);
ajax2.onreadystatechange = function () {
// Do some stuff
};
ajax2.send("id=1");
function sendAjax(url, data, callback) {
var ajax = objectoAjax();
ajax.open("POST", url, true);
ajax.onreadystatechange = callback;
ajax.send(data);
}
// AJAX request #1
sendAjax("page1.php", "foo=bar", function () {
// Do some stuff
});
sendAjax("page2.php", "id=1", function () {
// Do some stuff
});
Thanks for everything, If you have more than one AJAX task on your website, you should create ONE standard function for creating the XMLHttpRequest object, and call this for each AJAX task.
function objetoAjax(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}