Forum Moderators: open
<script type="text/javascript">
function OnSubmitBooking()
{
if(document.Booking.name.value == '')
{
divresult = document.getElementById('namebox')
divresult.className = 'validation'
divresult.innerHTML = 'Please fill in name';
document.Booking.name.focus();
return false;
}
if(document.Booking.passport.value == '')
{
divresult = document.getElementById('passportbox')
divresult.className = 'validation'
divresult.innerHTML = 'Please fill in your identification';
document.Booking.passport.focus();
return false;
}
if(document.Booking.emailtrue.value == '')
{
divresult = document.getElementById('emailtruebox');
divresult.className = 'validation'
divresult.innerHTML = 'Please fill in email';
document.Booking.emailtrue.focus();
return false;
}
if(document.Booking.repeat_email.value == '')
{
divresult = document.getElementById('repeat_emailbox');
divresult.className = 'validation'
divresult.innerHTML = 'Please repeat email';
document.Booking.repeat_email.focus();
return false;
}
if(document.Booking.telmobile.value == '')
{
divresult = document.getElementById('telmobilebox');
divresult.className = 'validation'
divresult.innerHTML = 'Please fill in mobile to bring on holiday';
document.Booking.telmobile.focus();
return false;
}
if(document.Booking.emailtrue.value != document.Booking.repeat_email.value)
{
divresult = document.getElementById('repeat_emailbox');
divresult.className = 'validation'
divresult.innerHTML = 'Email and repeat email are not equal';
return false;
}
if(document.Booking.test.value =='')
{
divresult = document.getElementById('resultbooking');
divresult.className = 'validation'
divresult.innerHTML = 'Please answer the security question';
document.Booking.test.focus();
return false;
}
if (isNaN(document.Booking.test.value))
{
divresult.className = 'validation'
divresult.innerHTML = 'The answer is invalid, use only numbers in security question';
document.Booking.test.focus();
return false;
}
else{
divresult.style.color="blue";
divresult.innerHTML = 'Please Wait...';}
}
</script>
function OnSubmitBooking()
{
if(document.Booking.name.value == '')
{
divresult = document.getElementById('namebox')
divresult.className = 'validation'
divresult.innerHTML = 'Please fill in name';
document.Booking.name.focus();
return false;
function quitarfocus()
{
divresult.innerHTML.style.display = "none"}
}
html:
<input id="name" name="name" title="Please enter your name" onchange="quitarfocus()" type="text" size="50">
<div id="namebox"></div>
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 OnSubmitBooking()
{
if(document.Booking.emailtrue.value != document.Booking.repeat_email.value)
{
divresult = document.getElementById('repeat_emailbox');
divresult.className = 'validation'
divresult.innerHTML = 'Email and repeat email are not equal';
document.Booking.repeat_email.focus();
return false;
}
var info = {
names : ['namebox' , 'Please fill in name' ],
passport : ['passportbox' , 'Please fill in your identification' ],
emailtrue : ['emailtruebox' , 'Please fill in email' ],
repeat_email: ['repeat_emailbox', 'Please repeat email' ],
telmobile : ['telmobilebox' , 'Please fill in mobile to bring on holiday' ],
test : ['resultbooking' , 'Please answer the security question' ]
}
for( key in info ) {
if(document.Booking[key].value == '')
{
divresult = document.getElementById(info[key][0])
divresult.className = 'validation'
divresult.innerHTML = info[key][1];
document.Booking[key].focus();
return false;
}
}
divresult = document.getElementById('resultbooking');
//recogemos los valores de los inputs
enviar = document.solicitud.enviar.value;
question = document.solicitud.question.value;
answer = document.solicitud.answer.value;
test = document.solicitud.test.value;
var ajaxcaptcha = objetoAjax();
ajaxcaptcha.open("POST", "captchaajax.php", true);
ajaxcaptcha.onreadystatechange = function () {
if (ajaxcaptcha.readyState == 4) {
divresult.innerHTML = ajaxcaptcha.responseText
<!-- LimpiarCampos();-->
}
}
ajaxcaptcha.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxcaptcha.send("test=" + test + "&enviar=" + enviar + "&answer=" + answer +
"&question=" + question + "");
return false;
} if (isset($_POST['enviar'])){
$question = $_POST['question'];
$answer = $_POST['answer'];
$test = $_POST['test'];
$enviar = $_POST['enviar'];
$result2 = $dbh->query("SELECT question, answer FROM captcha ORDER BY RAND() LIMIT 1");
if($row = $result2->fetch()) {
$question = $row["question"];
$answer = $row["answer"];
if ($test != $answer) {
echo "The answer of the security question is not correct";
}
}
}
enviar = document.solicitud.enviar.value;
question = document.solicitud.question.value;
answer = document.solicitud.answer.value;
test = document.solicitud.test.value;
var ajaxcaptcha = objetoAjax();
ajaxcaptcha.open("POST", "/pasarela/captchaajax.php", true);
ajaxcaptcha.onreadystatechange = function () {
if (ajaxcaptcha.readyState == 4) {
if (ajaxcaptcha.responseText === "ok") {
return true;
} else {
divresult.innerHTML = "The answer to the security question was not correct"
<!-- LimpiarCampos();-->
}
}
}
ajaxcaptcha.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxcaptcha.send("test=" + test + "&enviar=" + enviar + "&answer=" + answer +
"&question=" + question + "");
return false;
} if (isset($_POST['enviar'])){
$answer = $_POST['answer'];
$test = $_POST['test'];
$enviar = $_POST['enviar'];
if ($test = $answer) {
echo "ok";
}
}
Tried to add the onchange function inside the other function in the first input but does not work:
function OnSubmitBooking()
{
...
}
function quitarfocus()
{
...
}
ajaxcaptcha.open("POST", "/pasarela/captchaajax.php", false);
if (ajaxcaptcha.readyState == 4) {
if (ajaxcaptcha.responseText === "ok") {
return true;
} else {
divresult.innerHTML = "The answer to the security question was not correct"
<!-- LimpiarCampos();-->
}
}
Alternatively, you could always return false from your onsubmit method, then in the AJAX request, call form.submit() if the request is successful.
This is been trying, been reeeding and if I understood correctly instead of true I declare false here:
ajaxcaptcha.open("POST", "captchaajax.php", false);
function OnSubmitBooking() {
...
ajaxcaptcha.open("POST", "captchaajax.php", false);
ajaxcaptcha.onreadystatechange = function () {
if (ajaxcaptcha.readyState == 4) {
if (ajaxcaptcha.responseText === "ok") {
document.Booking.submit();
} else {
divresult.innerHTML = "The answer to the security question was not correct"
<!-- LimpiarCampos();-->
}
}
};
return false; // always return false here to prevent default form submit
}
function OnSubmitBooking() {
...
ajaxcaptcha.open("POST", "captchaajax.php", true); // async
ajaxcaptcha.onreadystatechange = function () {
if (ajaxcaptcha.readyState == 4) {
if (ajaxcaptcha.responseText === "ok") {
document.Booking.submit();
} else {
divresult.innerHTML = "The answer to the security question was not correct"
<!-- LimpiarCampos();-->
}
}
};
return false; // always return false here to prevent default form submit
}
var enviar = document.createElement('input');
enviar.type = 'hidden';
enviar.name = 'enviar';
enviar.value = true;
document.Booking.appendChild(enviar);
document.Booking.submit();
Also, ideally you should not use inline event handlers. Instead, it's better to assign the event handlers from the JavaScript. So instead of this:
<input id="name" name="name" title="Please enter your name" onchange="quitarfocus()" type="text" size="50">
You could do something like this within your script:
var n = document.getElementById('name');
n.onchange = quitarfocus;
Even better, though, you might attach a single onchange event handler to a parent element of all your inputs, so you only need 1 handler total instead of 1 for each element. Your handler would then need to inspect the event to see which input field triggered it.
var n = document.getElementById('repeat_email');
n.onchange = quitarfocus();
function quitarfocus()
{
document.getElementById("repeat_emailbox").style.display = "none";
}
function OnSubmitBooking()
{
if(document.Booking.emailtrue.value != document.Booking.repeat_email.value)
{ function quitarfocus()
{
var n = document.getElementById('repeat_email');
n.onchange = quitarfocus();
document.getElementById("repeat_emailbox").style.display = "none";
}
function OnSubmitBooking()
{
if(document.Booking.emailtrue.value != document.Booking.repeat_email.value)
{
document.getElementById('repeat_emailbox').style.display = "block"; <input id="repeat_email" onchange="quitarfocus()" name="repeat_email" title="Please repeat your email" size="61" type="text">
<div id="repeat_emailbox"></div>
function quitarfocus()
{
var n = document.getElementById('repeat_email');
if (n != '')
{
document.getElementById("repeat_emailbox").style.display = "none";
}
}
function OnSubmitBooking()
{
if(document.Booking.emailtrue.value != document.Booking.repeat_email.value)
{
document.getElementById('repeat_emailbox').style.display = "block"; 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 quitarfocus()
{
var n = document.getElementById('repeat_email');
if (n != '')
{
document.getElementById("repeat_emailbox").style.display = "none";
}
}
function OnSubmitBooking()
{
if(document.Booking.emailtrue.value != document.Booking.repeat_email.value)
{
document.getElementById('repeat_emailbox').style.display = "block";
divresult = document.getElementById('repeat_emailbox');
divresult.className = 'validation'
divresult.innerHTML = 'Email and repeat email are not equal';
document.Booking.repeat_email.focus();
return false;
}
var info = {
names : ['namebox' , 'Please fill in name' ],
passport : ['passportbox' , 'Please fill in your identification' ],
emailtrue : ['emailtruebox' , 'Please fill in email' ],
repeat_email: ['repeat_emailbox', 'Please repeat email' ],
telmobile : ['telmobilebox' , 'Please fill in mobile to bring on holiday' ],
test : ['resultbooking' , 'Please answer the security question' ]
}
for( key in info ) {
if(document.Booking[key].value == '')
{
divresult = document.getElementById(info[key][0])
divresult.className = 'validation'
divresult.innerHTML = info[key][1];
document.Booking[key].focus();
return false;
}
}
divresult = document.getElementById('resultbooking');
var enviar = document.Booking.enviar.value,
question = document.Booking.question.value,
answer = document.Booking.answer.value,
test = document.Booking.test.value;
var ajaxcaptcha = objetoAjax();
ajaxcaptcha.open("POST", "captchaajax.php", true);
ajaxcaptcha.onreadystatechange = function () {
if (ajaxcaptcha.readyState==1 || ajaxcaptcha.readyState==2 || ajaxcaptcha.readyState==3)
{
divresult.className = 'validationok'
divresult.innerHTML = 'Please wait...';
}
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();
} else {
divresult.className = 'validation'
divresult.innerHTML = ajaxcaptcha.responseText
}
}
}
ajaxcaptcha.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxcaptcha.send("test=" + test + "&enviar=" + enviar + "&answer=" + answer +
"&question=" + question + "");
return false;
}