Forum Moderators: open
percentage = document.solicitud.percentage.checked.value();
percentage = document.solicitud.percentage[0].value;
for( i = 0; i < document.solicitud.percentage.length; i++ )
{
if( document.solicitud.percentage[i].checked == true ) { val = document.solicitud.percentage[i].value; // stop searching as soon as one is found
} }
alert( "val = " + val );
function getvaulue() {
theForm = document.getElementById('solicitud');
radiopercentage = theForm.elements['percentage'];
for (var i=0; i < radiopercentage.length; i++) {
if (radiopercentage[i].checked) radiopercentage[i].value;
}
}
if (document.solicitud.percentage[i].checked == true ){
percentage = document.solicitud.percentage.value;}
function enviarDatosSolicitud(){
divResultado.style.display = "block";
propiedad = document.solicitud.propiedad.value;
terms = document.solicitud.terms.value;
day = document.solicitud.day.options[document.solicitud.day.selectedIndex].value;
day2 = document.solicitud.day2.options[document.solicitud.day2.selectedIndex].value;
month = document.solicitud.month.options[document.solicitud.month.selectedIndex].value;
month2 = document.solicitud.month2.options[document.solicitud.month2.selectedIndex].value;
for( i = 0; i < document.solicitud.percentage.length; i++ )
{
if( document.solicitud.percentage[i].checked == true ) { percentage = document.solicitud.percentage[i].value; // stop searching as soon as one is found
} }
function enviarDatosSolicitud(){
var divResultado = document.getElementById('resultado');
divResultado.style.display = "block";
propiedad = document.solicitud.propiedad.value;
terms = document.solicitud.propiedad.value;
for( i = 0; i < document.solicitud.percentage.length; i++ )
{
if( document.solicitud.percentage[i].checked == true ) { percentage = document.solicitud.percentage[i].value; // stop searching as soon as one is found
} }
day = document.solicitud.day.options[document.solicitud.day.selectedIndex].value;
day2 = document.solicitud.day2.options[document.solicitud.day2.selectedIndex].value;
month = document.solicitud.month.options[document.solicitud.month.selectedIndex].value;
month2 = document.solicitud.month2.options[document.solicitud.month2.selectedIndex].value;
if (!empty($percentage) && !empty($terms)) {
if ($salida > $llegada){
function enviarDatosSolicitud(){
var divResultado = document.getElementById('resultado');
divResultado.style.display = "block";
propiedad = document.solicitud.propiedad.value;
terms = document.solicitud.terms.value;
for( i = 0; i < document.solicitud.percentage.length; i++ )
{
if( document.solicitud.percentage[i].checked == true ) { percentage = document.solicitud.percentage[i].value; // stop searching as soon as one is found
} }
day = document.solicitud.day.options[document.solicitud.day.selectedIndex].value;
day2 = document.solicitud.day2.options[document.solicitud.day2.selectedIndex].value;
month = document.solicitud.month.options[document.solicitud.month.selectedIndex].value;
month2 = document.solicitud.month2.options[document.solicitud.month2.selectedIndex].value;
terms = document.solicitud.terms.value;
for( i = 0; i < document.solicitud.percentage.length; i++ )
{
if( document.solicitud.percentage[i].checked == true ) { percentage = document.solicitud.percentage[i].value; // stop searching as soon as one is found
} }
<form name="solicitud" class="formenquirer" id="solicitud" action="page.php?propiedad=<?php echo $propiedad; ?>" method="post">
<input name="percentage" class="percentage" type="radio" value="25">25% as a deposit and balance 8 weeks before arrival <br>
<input name="percentage" class="percentage" type="radio" value="50">50% as a deposit and balance on arrival</p>
<input name="terms" type="checkbox" title="Please accept terms" value="terms">
<input class="boton roundedcorner border" size="10" type="submit" onClick="enviarDatosSolicitud();" name="enviar" id="enviar" value="Book Now">
function enviarDatosSolicitud(){
var i,
divResultado = document.getElementById('resultado'),
f = document.solicitud,
propiedad = f.propiedad.value,
terms = f.terms.value,
percentage = null,
day = f.day.options[f.day.selectedIndex].value,
day2 = f.day2.options[f.day2.selectedIndex].value,
month = f.month.options[f.month.selectedIndex].value,
month2 = f.month2.options[f.month2.selectedIndex].value;
divResultado.style.display = "block";
for (i = 0; i < f.percentage.length; i++ ) {
if (f.percentage[i].checked) {
percentage = f.percentage[i].value;
break; // stop searching as soon as one is found
}
}
// ... whatever you need to do with these values
return false; // prevent form from submitting
}
You don't want to handle this in the onClick of the submit button, you want to handle it in the form onsubmit handler so you can prevent the form from submitting (if you're submitting it via AJAX instead). I suspect the reason why you're seeing inconsistent results is because you have a race condition between the onclick of the submit button and the onsubmit of the form, where the form is continuing the submit process (thus trashing your existing document).
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 dataChanged() {
document.getElementById("resultado").style.display = "none";
}
function enviarDatosSolicitud(){
if ( ( document.solicitud.terms.checked == false ) )
{ alert ( "Please accept terms" );
}
if ( ( document.solicitud.percentage[0].checked == false ) && ( document.solicitud.percentage[1].checked == false ) )
{ alert ( "Please choose payment option" );
}
var divResultado = document.getElementById('resultado');
divResultado.style.display = "block";
propiedad = document.solicitud.propiedad.value;
terms = document.solicitud.terms.value;
for( i = 0; i < document.solicitud.percentage.length; i++ )
{
if( document.solicitud.percentage[i].checked == true ) { percentage = document.solicitud.percentage[i].value; // stop searching as soon as one is found
}
}
day = document.solicitud.day.options[document.solicitud.day.selectedIndex].value;
day2 = document.solicitud.day2.options[document.solicitud.day2.selectedIndex].value;
month = document.solicitud.month.options[document.solicitud.month.selectedIndex].value;
month2 = document.solicitud.month2.options[document.solicitud.month2.selectedIndex].value;
ajax=objetoAjax();
ajax.open("POST", "page.php",true);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
if ( ajax.responseText === "Loading, please wait" ) {
window.location='page.php';
}
else {
divResultado.innerHTML = ajax.responseText
LimpiarCampos();
}
}
}
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send("propiedad="+propiedad+"&enviar="+enviar+"&day="+day+"&day2="+day2+"&month="+month+"&month2="+month2+"&percentage="+percentage+"&terms="+terms+"")
}
document.getElementById('enviar').type = 'button';
function enviarDatosSolicitud() {
var divResultado = document.getElementById('resultado'),
valid = true,
invalidStr = '';
if (!document.solicitud.terms.checked) {
invalidStr += "<div>Please accept terms.<\/div>";
valid = false;
}
if ((!document.solicitud.percentage[0].checked) &&
(!document.solicitud.percentage[1].checked)) {
invalidStr += "<div>Please choose payment option.<\/div>";
valid = false;
}
divResultado.style.display = "block";
if (!valid) {
divResultado.innerHTML = invalidStr;
return; // stop right here
}
var propiedad = document.solicitud.propiedad.value,
terms = document.solicitud.terms.value,
day = document.solicitud.day.options[document.solicitud.day.selectedIndex].value,
day2 = document.solicitud.day2.options[document.solicitud.day2.selectedIndex].value,
month = document.solicitud.month.options[document.solicitud.month.selectedIndex].value,
month2 = document.solicitud.month2.options[document.solicitud.month2.selectedIndex].value;
for (i = 0; i < document.solicitud.percentage.length; i++) {
if (document.solicitud.percentage[i].checked) {
percentage = document.solicitud.percentage[i].value;
break; // stop searching as soon as one is found
}
}
ajax = objetoAjax();
ajax.open("POST", "page.php", true);
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (ajax.responseText === "Loading, please wait") {
window.location = 'page.php';
} else {
divResultado.innerHTML = ajax.responseText
LimpiarCampos();
}
}
}
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("propiedad=" + propiedad + "&enviar=" + enviar + "&day=" + day +
"&day2=" + day2 + "&month=" + month + "&month2=" + month2 +
"&percentage=" + percentage + "&terms=" + terms + "");
}
function enviarDatosSolicitud() {
var divResultado = document.getElementById('resultado'),
valid = true,
invalidStr = '';
if (!document.solicitud.terms.checked) {
invalidStr += "<div>Please accept terms.<\/div>";
valid = false;
}
if ((!document.solicitud.percentage[0].checked) &&
(!document.solicitud.percentage[1].checked)) {
invalidStr += "<div>Please choose payment option.<\/div>";
valid = false;
}
divResultado.style.display = "block";
if (!valid) {
divResultado.innerHTML = invalidStr;
etc....
However, what if the user hits the Enter key instead of
clicking the button?
Yes, similar to what I did in my example, which was to initialize it to null, and then only update it if it's checked.
<form name="solicitud" class="formenquirer" id="solicitud" action="page.php?propiedad=<?php echo $propiedad; ?>" onsubmit="return enviarDatosSolicitud();" method="post">
<input type="hidden" id="propiedad" name="propiedad" value="<?php print($propiedad); ?>">
<p>Arrival: <?php include '../includes/day.php'; ?><?php include '../includes/month.php'; ?>
Departure: <?php include '../includes/day2.php'; ?><?php include '../includes/month2.php'; ?>
<input name="percentage" class="percentage" type="radio" value="25">25% as a deposit and balance 8 weeks before arrival <br>
<input name="percentage" class="percentage" type="radio" value="50">50% as a deposit and balance on arrival
<input name="terms" type="checkbox" title="Please accept terms" value="terms">
<input class="boton roundedcorner border" size="10" type="submit" name="enviar" id="enviar" value="Book Now">
<div id="resultado"></div>
</form>
<script type="text/javascript" src="/ajax.js"></script>
Using this in the form element:
onsubmit="return enviarDatosSolicitud();"
function enviarDatosSolicitud() {
var divResultado = document.getElementById('resultado'),
valid = true,
invalidStr = '';
if (!document.solicitud.terms.checked) {
invalidStr += "<div>Please accept terms.<\/div>";
valid = false;
}
if ((!document.solicitud.percentage[0].checked) &&
(!document.solicitud.percentage[1].checked)) {
invalidStr += "<div>Please choose payment option.<\/div>";
valid = false;
}
divResultado.style.display = "block";
if (!valid) {
divResultado.innerHTML = invalidStr;
return false; // stop right here
}
var propiedad = document.solicitud.propiedad.value,
terms = document.solicitud.terms.value,
day = document.solicitud.day.options[document.solicitud.day.selectedIndex].value,
day2 = document.solicitud.day2.options[document.solicitud.day2.selectedIndex].value,
month = document.solicitud.month.options[document.solicitud.month.selectedIndex].value,
month2 = document.solicitud.month2.options[document.solicitud.month2.selectedIndex].value;
for (i = 0; i < document.solicitud.percentage.length; i++) {
if (document.solicitud.percentage[i].checked) {
percentage = document.solicitud.percentage[i].value;
break; // stop searching as soon as one is found
}
}
ajax = objetoAjax();
ajax.open("POST", "page.php", true);
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (ajax.responseText === "Loading, please wait") {
window.location = 'page.php';
} else {
divResultado.innerHTML = ajax.responseText
LimpiarCampos();
}
}
}
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("propiedad=" + propiedad + "&enviar=" + enviar + "&day=" + day +
"&day2=" + day2 + "&month=" + month + "&month2=" + month2 +
"&percentage=" + percentage + "&terms=" + terms + "");
return false
}
Make sure your enviarDatosSolicitud function returns false (to prevent the form from submitting), as updated here: