Ver Mensaje Individual
  #6 (permalink)  
Antiguo 30/03/2010, 13:56
alvarols
 
Fecha de Ingreso: diciembre-2008
Mensajes: 738
Antigüedad: 15 años, 5 meses
Puntos: 15
Respuesta: Problema al mandar mail con Ajax.

Cita:
Iniciado por eulloa Ver Mensaje
Pregunta tonta: ¿está creado el div #response en tu página?
Si, está creado, es este:

<div id="response"></div> Y lo puse en la parte superior del formulario de correo

cambié el html por el text y nada.

El script lo saqué de un código de un tutorial. El código original si está funcionando, pero no se porque al adaptarlo no me funciona.

Este es el formulario:

http://lapanzaesprimero.net/contacto.php

Si quieres te pongo el código del Ajaxmail (que es la aplicación que si sirve, a ver si sirve de referencia).

INDEX.PHP
Código:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Send a mail with php and ajax using jquery</title>
	<link rel="stylesheet" type="text/css" href="ajax.css" />
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript" src="ajax.js"></script>
</head>

<body>

<div id="wrap">
	<h1>Send a mail using jquery, php and Ajax</h1>
	<h4>By <a href="http://lastwebdesigner.com" title="web design resources">LastWebDesigner</a></h4>
	<div class="mail">
			<div id="response">
		
		</div>
		<form id="formail" action="" method ="post">
			<label>Name : </label>
			<input type="text" name="name" id="name" />
			<label>Your mail :</label>
			<input type="text" name="mail" id="mail" />
			<label>Subject : </label>
			<input type="text" name="subject" id="subject" />
			<label>Text :</label>
			<textarea name="text" id="text" cols="40" rows="10"></textarea> 
			<input type="submit" value="send mail" id="sendmail" name="sendmail" />
			</form>
	</div>

</div>

</body>
</html>
MAIL.PHP

Código:
<?php
	$mail = $_POST['mail'];
	$name = $_POST['name'];
	$subject = $_POST['subject'];
	$text = $_POST['text'];
	
 $to = "[email protected]";
 $message =" You received  a mail from ".$mail;
 $message .=" Text of the message : ".$text;

 if(mail($to, $subject,$message)){
	echo "mail successful send";
} 
else{ 
	echo "there's some errors to send the mail, verify your server options";
}
?>
AJAX.JS

Código:
 $(document).ready(function(){
	$("#sendmail").click(function(){
		var valid = '';
		var isr = ' is required.';
		var name = $("#name").val();
		var mail = $("#mail").val();
		var subject = $("#subject").val();
		var text = $("#text").val();
		if (name.length<1) {
			valid += '<br />Name'+isr;
		}
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += '<br />A valid Email'+isr;
		}
		if (subject.length<1) {
			valid += '<br />Subject'+isr;
		}
		if (text.length<1) {
			valid += '<br />Text'+isr;
		}
		if (valid!='') {
			$("#response").fadeIn("slow");
			$("#response").html("Error:"+valid);
		}
		else {
			var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text;
			$("#response").css("display", "block");
			$("#response").html("Sending message .... ");
			$("#response").fadeIn("slow");
			setTimeout("send('"+datastr+"')",2000);
		}
		return false;
	});
});
function send(datastr){
	$.ajax({	
		type: "POST",
		url: "mail.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#response").fadeIn("slow");
		$("#response").html(html);
		setTimeout('$("#response").fadeOut("slow")',2000);
	}
	});
}