Foros del Web » Programando para Internet » Javascript » Frameworks JS »

[SOLUCIONADO] formulario ajax con mootools, funciona a medias

Estas en el tema de formulario ajax con mootools, funciona a medias en el foro de Frameworks JS en Foros del Web. Hola, Primero no sé si esto debe de ir aquí o en php. Tengo un script php que calcula precios, funciona. Pero para que no ...
  #1 (permalink)  
Antiguo 21/12/2013, 14:30
 
Fecha de Ingreso: mayo-2009
Mensajes: 382
Antigüedad: 14 años, 10 meses
Puntos: 6
formulario ajax con mootools, funciona a medias

Hola,
Primero no sé si esto debe de ir aquí o en php.
Tengo un script php que calcula precios, funciona.
Pero para que no haya que refrescar la pagina para calcular estoy intentando hacerlo con ajax, y he elegido mootools como ya uso la libreria para un slideshow.
He seguido las instrucciones de este manual:
http://www.desarrolloweb.com/articul...-mootools.html

El ejemplo funciona perfectamente en mi web.
Y para usarlo con mi script he cambiado el formulario etc.

Este es el formulario y html:
Código:
<div class="calculator">
			<?php
			 $llegada = htmlspecialchars($_POST['llegada']); 
$salida = htmlspecialchars($_POST['salida']);
$day = htmlspecialchars($_POST['day']);
$day2 = htmlspecialchars($_POST['day2']);
$month = htmlspecialchars($_POST['month']);
$month2 = htmlspecialchars($_POST['month2']);
  ?>
        <form id="miformulario" action="calculatornew.php" method="post">
        <p class="formheader">Check availability and price for this property: </p>
		<p class="calculatortext">Arrival: <?php include("includes/day.php"); ?>&nbsp;&nbsp;<?php include("includes/month.php"); ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Departure: <?php include("includes/day2.php"); ?>&nbsp;&nbsp;<?php include("includes/month2.php"); ?> </p>
<p><input class="boton roundedcorner border" size="10" type="submit" name="enviar" value="Calculate/Check">
</p>
</form>  
<div id="resultado"></div>
</div><!-- end form -->
en la pagina calculatornew.php hay un script que usa mysql y hace varios calculos
y al final hay un echo asi:
Código PHP:
            <?php
mysql_close
($dbh);
echo 
"<p class=\"resulttext\">Available: $availability<br>Price in euros: <span class=\"red\">$half_round</span><br>Nights: <span class=\"red\">$dias</span></p>";
?>
Problema, cuando le doy a enviar el botón escribe perfectamente los echos, pero el php o mysql no se ha ejecutado,
simplemente escribe asi:
Available:
Price in euros:
Nights:
es decir sin resultado ninguno.
Sin embargo el ejemplo con php en el manul me funciona.
No debería de ejecutarse todo lo que hay en la pagina .php o se debe de hacer el javascript de otra manera?
Gracias
  #2 (permalink)  
Antiguo 21/12/2013, 14:39
 
Fecha de Ingreso: mayo-2009
Mensajes: 382
Antigüedad: 14 años, 10 meses
Puntos: 6
Respuesta: formulario ajax con mootools, funciona a medias

Si quito esto en la pagina php:
if ($_POST['enviar']) {
entonces se ejecuta y me da un error.
No se envia las variables por el post me parece.
  #3 (permalink)  
Antiguo 21/12/2013, 16:53
 
Fecha de Ingreso: mayo-2009
Mensajes: 382
Antigüedad: 14 años, 10 meses
Puntos: 6
Respuesta: formulario ajax con mootools, funciona a medias

puf,
me estoy acercando,
no entiendo, me imagino que basta que paso todas las variables a la pagina php que se usa en el formulario, y aun asi no me funciona.
Mi script php pita y dice que la fecha es incorrecta.
las fechas las tengo en un select, quizas es por eso?
De momento tengo esto:
<script type="text/javascript">
var xmlhttp;
function load(str, url, cfunc)
{

if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(str);
}

function metodoAjax(datos, ruta)
{

load(datos, ruta, function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("cuerpo").innerHTML=xmlhtt p.responseText;
}
});
}
//------------------------------------------------------------------
function recibe(){
day = document.getElementById('day').value;
metodoAjax("valor="+day,"calculatornew.php");
day2 = document.getElementById('day2').value;
metodoAjax("valor="+day2,"calculatornew.php");
month = document.getElementById('month').value;
metodoAjax("valor="+month,"calculatornew.php");
month2 = document.getElementById('month2').value;
metodoAjax("valor="+month2,"calculatornew.php");
propiedad = document.getElementById('propiedad').value;
metodoAjax("valor="+propiedad,"calculatornew.php") ;
enviar = document.getElementById('enviar').value;
metodoAjax("valor="+enviar,"calculatornew.php");
}
</script>
<form method="post">
<input type="text" id="propiedad" value="<?php print($propiedad); ?>">
<p class="formheader">Check availability and price for this property: </p>
<p class="calculatortext">Arrival: <?php include("includes/day.php"); ?>&nbsp;&nbsp;<?php include("includes/month.php"); ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Departure: <?php include("includes/day2.php"); ?>&nbsp;&nbsp;<?php include("includes/month2.php"); ?> </p>
<p><input class="boton roundedcorner border" size="10" type="button" onClick="recibe();" name="enviar" id="enviar" value="Calculate/Check">
</p>
</form>
<div id="cuerpo"></div>
</div><!-- end form -->
  #4 (permalink)  
Antiguo 22/12/2013, 03:12
 
Fecha de Ingreso: mayo-2009
Mensajes: 382
Antigüedad: 14 años, 10 meses
Puntos: 6
Respuesta: formulario ajax con mootools, funciona a medias

lo consegúi buscando y haciendo ajuste,
aunque no sé si es lo correcto:
<form name="solicitud" id="solicitud" method="post">
<input type="hidden" id="propiedad" value="<?php print($propiedad); ?>">
<p class="formheader">Check availability and price for this property: </p>
<p class="calculatortext">Arrival: <?php include("includes/day.php"); ?>&nbsp;&nbsp;<?php include("includes/month.php"); ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Departure: <?php include("includes/day2.php"); ?>&nbsp;&nbsp;<?php include("includes/month2.php"); ?> </p>
<p><input class="boton roundedcorner border" size="10" type="button" onClick="enviarDatosSolicitud();" name="enviar" id="enviar" value="Calculate/Check">
</p>
</form>
<div id="resultado"></div>
<script type="text/javascript">
// Función para recoger los datos de PHP según el navegador, se usa siempre.
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 enviarDatosSolicitud(){

//div donde se mostrará lo resultados
divResultado = document.getElementById('resultado');
//recogemos los valores de los inputs
propiedad = document.solicitud.propiedad.value;
enviar = document.solicitud.enviar.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;

//instanciamos el objetoAjax
ajax=objetoAjax();

//uso del medotod POST
//archivo que realizará la operacion
//registro.php
ajax.open("POST", "calculatornew.php",true);
//cuando el objeto XMLHttpRequest cambia de estado, la función se inicia
ajax.onreadystatechange=function() {
//la función responseText tiene todos los datos pedidos al servidor
if (ajax.readyState==4) {
//mostrar resultados en esta capa
divResultado.innerHTML = ajax.responseText
//llamar a funcion para limpiar los inputs
LimpiarCampos();
}
}
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//enviando los valores a registro.php para que inserte los datos
ajax.send("propiedad="+propiedad+"&enviar="+enviar +"&day="+day+"&day2="+day2+"&month="+month+"&month 2="+month2+"")
}
</script>
  #5 (permalink)  
Antiguo 22/12/2013, 04:33
 
Fecha de Ingreso: mayo-2009
Mensajes: 382
Antigüedad: 14 años, 10 meses
Puntos: 6
Respuesta: formulario ajax con mootools, funciona a medias

y tambien cambien el type="button" a submit para que funcione si no hay javascript
y cambie el boton si hay javascript:
document.getElementById('enviar').type = 'button';

Etiquetas: ajax
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:34.