Welcome to WebmasterWorld Guest from 3.80.4.76

Forum Moderators: open

Message Too Old, No Replies

submit form with javascript, only works if validation fails.

     
9:17 am on Mar 25, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


Hi,
Fotiman helped me to submit a form using javascript.
I just discovered that if the form is correctly fillt in, the form is not submitted.
How can I work around this?

this is the relevant html:
 <form class="instant" name="Booking" id="Booking" 
action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post" onSubmit="return OnSubmitBooking();">
....
<input class="boton roundedcorner border" type="submit" name="enviar" value="Confirm booking and pay deposit now"></p>


And this is the javascript/Ajax:
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;
}

//Función para recoger los datos del formulario y enviarlos por post
function enviarDatoscupon(){
//div donde se mostrará lo results
divresult = document.getElementById('result');
//recogemos los valores de los inputs

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;
var ajaxcupones = objetoAjax();
ajaxcupones.open("POST", "/pasarela/cuponesbookingajaxsve.php", true);
ajaxcupones.onreadystatechange = function () {
if (ajaxcupones.readyState==1 || ajaxcupones.readyState==2 || ajaxcupones.readyState==3)
{
divresult.innerHTML = 'Var god vänta...';
}
if (ajaxcupones.readyState == 4) {
if (ajaxcupones.responseText === "Var god vänta") {
window.location = '/svenska/pasarela/bokningsomedelbarfortsattning.php';
} else {
divresult.innerHTML = ajaxcupones.responseText
<!-- LimpiarCampos();-->
}
}
}
ajaxcupones.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxcupones.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;
}
9:19 am on Mar 25, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


Stupid me,
As this is ever true if the input with Ajax is correctly fillt in, the form submit is ever done....
if (ajaxcaptcha.responseText === "Loading, please wait") {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}
9:37 am on Mar 25, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


I added this:
if (ajaxcaptcha.responseText === "") {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}


So know I have this:
if (ajaxcaptcha.readyState == 4) {
if (ajaxcaptcha.responseText === "Loading, please wait") {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}
if (ajaxcaptcha.responseText === "") {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}else {
divresult.className = 'validation'
divresult.innerHTML = ajaxcaptcha.responseText
}
}
}
10:25 am on Mar 25, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


This was not a good idea...
using that the form is submitted when it should not validate
So know I have this:
if (ajaxcaptcha.readyState == 4) {
if (ajaxcaptcha.responseText === "Loading, please wait") {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}
if (ajaxcaptcha.responseText === "") {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}else {
divresult.className = 'validation'
divresult.innerHTML = ajaxcaptcha.responseText
}
}
}
10:37 am on Mar 25, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


if (ajaxcaptcha.readyState == 4) {
if (ajaxcaptcha.responseText === "Loading, please wait") {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}
if (ajaxcaptcha.responseText === "The answer to the security question is not correct") {
divresult.className = 'validation'
divresult.innerHTML = ajaxcaptcha.responseText
}
else {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}
}
}


This seems to be correct or at least it works:
if (ajaxcaptcha.readyState == 4) {
if (ajaxcaptcha.responseText === "Loading, please wait") {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}
if (ajaxcaptcha.responseText === "The answer to the security question is not correct") {
divresult.className = 'validation'
divresult.innerHTML = ajaxcaptcha.responseText
}
else {
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
}
}
}
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members