Ver Mensaje Individual
  #1 (permalink)  
Antiguo 21/10/2009, 05:23
FraNciS31
 
Fecha de Ingreso: mayo-2009
Mensajes: 19
Antigüedad: 15 años
Puntos: 0
$.ajax() no me envia los datos

Buenas,
no se si este post debería estar en este apartado del foro ya que trata de PHP, jQuery & AJAX, pero el problema viene de un script con jQuery en el que quiero utilizar la funcion $.ajax() donde valido un formulario para despues enviar email.
Si envio el formulario PHP sin JavaScript me envia los emails con la funcion mail(), pero si activo JavaScript me dice que el formulario se envió correctamente pero en cambio nunca recibo ningun email.
Estoy dandole vueltas al asunto pero no logro saber donde esta el fallo. Si alguien me puede ayudar lo agradecería mucho ya que me urge bastante.
Aqui dejo el script completo PHP y JavaScript.
Saludos.

<?php //Form Handling PHPScript
include ('includes/header.html');

if (isset ($_POST['submitted'])) {

$errors = array();

if (empty($_POST['UsrName'])) {
$errors[] = 'You forgot to enter your name.';
} else {
$name = trim($_POST['UsrName']);
}

if (empty($_POST['UsrMail'])) {
$errors[] = 'You forgot to enter your email address.';
} else if (!preg_match( trim($_POST['UsrMail']))) {
$errors[] = 'Your Email address is not valid.';
} else {
$email = trim($_POST['UsrMail']);
}

if (empty($_POST['UsrMessage'])){
$errors[] = 'You didn\'t write any comments.';
} else {
$comments = trim($_POST['UsrMessage']);
}


if (empty($errors)) {
$body = "Name:{$_POST['UsrName']}\n\nComments:{$_POST['UsrMessage']}";
$body = wordwrap($body, 70);
mail('midominio', 'Contact Form', $body, "From:{$_POST['UsrMail']}");

echo "<h1 class='notified'>Thank You!</h1>
<h2>Your form has been submitted.</h2>
<p>I will reply to you at <i>$email</i>.</p></div>\n";

include ('includes/footer.html');
exit();

} else {
echo '<h1 class="notified">Excuse Me</h1><p class="error">Your form is not valid.<br/>';
foreach ($errors as $msg) {
echo "$msg<br/>\n";
} echo '</p><p class="error">Please fill in the form again.</p>';

}

}

?>

<div id="contactForm">
<h1>Contact</h1>
<form action="psdtoxhtml-contact.php" method="post" id="frmContact">
<p>
<label for="usrName">Your Name</label>
<input type="text" id="usrName" class="text" name="UsrName" size="25" tabindex="1" value="<?php if (isset($_POST['UsrName'])) echo $_POST['UsrName'] ?>"/>
</p>
<p>
<label for="usrMail">Your E-Mail</label>
<input type="text" id="usrMail" class="text" name="UsrMail" size="25" tabindex="2" value="<?php if (isset($_POST['UsrMail'])) echo $_POST['UsrMail'] ?>"/>
</p>
<p>
<label for="usrMessage">Your Message</label>
<textarea id="usrMessage" class="text" name="UsrMessage" cols="40" rows="10" tabindex="3"><?php if (isset($_POST['UsrMessage'])) echo $_POST['UsrMessage'] ?></textarea>
</p>
<p>
<input type="hidden" name="submitted" value="1"/>
</p>
<p class="button">
<input type="submit" id="button" value="Send"/>
</p>
</form>
</div>
</div>

<div id="sidebar" >
<h2>Have your say</h2>
<p class="sidebar1">If you have any questions about anything I do,
or If you'd like to talk with me about a project you have,
please feel free to drop me a line and I'll get in touch as soon as I can.<br/><br/>
Hope to hear from you.
</p>
</div>

<?php
include('includes/footer.html');
?>


---jQuery----

$(document).ready(function() {
$('input#usrName').focus();
$('.errorName, .errorMail, .validMail, .errorMessage').hide();
$('#content').height(439);


$('#button').click(function() {

$('.errorName, .errorMail, .validMail, .errorMessage').hide();
var UsrName = $('input#usrName').val();
if (UsrName == "") {
$('input#usrName').after('<strong class="errorName">This field is required</strong>').fadeIn();
$('input#usrName').focus();
return false;
}

var UsrMail = $('input#usrMail').val();
if (UsrMail == "") {
$('input#usrMail').after('<strong class="errorMail">This field is required</strong>').fadeIn();
$('input#usrMail').focus();
return false;
}
else if (!emailReg.test(UsrMail)) {
$('input#usrMail').after('<strong class="validMail">Enter a valid email address</strong>').fadeIn();
$('input#usrMail').focus();
return false;
}

var UsrMessage = $('textarea#usrMessage').val();
if (UsrMessage == "") {
$('textarea#usrMessage').after('<strong class="errorMessage">This field is required</strong>').fadeIn();
$('textarea#usrMessage').focus();
return false;
}

var dataString = 'UsrName='+ UsrName + '&UsrMail=' + UsrMail + '&UsrMessage=' +UsrMessage;
//alert (dataString);return false;

$.ajax ({
type: "POST",
url: "contact.php",
data: dataString,
cache: false,
success: function() {
$('#contactForm').html("<div id='message'></div>").height(439);
$('#message').html("<h1>Thank You</h1>").append("<h2>Your contact form has been submitted!</h2><p>I will reply at " + UsrMail + " as soon as possible.</p><br/> ")
.hide()
.fadeIn(2000);
}

});
return false;