Foros del Web » Programando para Internet » Javascript »

Problemas con un formulario de contacto en Javascript y PHP

Estas en el tema de Problemas con un formulario de contacto en Javascript y PHP en el foro de Javascript en Foros del Web. Estoy tratando de implementar un formulario en mi página, es este: http://www.elated.com/res/File/artic...rm-jquery-php/ El problema es que cuando mando un mensaje, el formulario se queda colgado ...
  #1 (permalink)  
Antiguo 18/09/2012, 11:28
 
Fecha de Ingreso: diciembre-2008
Mensajes: 738
Antigüedad: 15 años, 4 meses
Puntos: 15
Problemas con un formulario de contacto en Javascript y PHP

Estoy tratando de implementar un formulario en mi página, es este:

http://www.elated.com/res/File/artic...rm-jquery-php/

El problema es que cuando mando un mensaje, el formulario se queda colgado con el mensaje "enviando correo" y no dice que el correo ya se ha enviado. Aunque los correos si llegan. En la página que les mostré si se hace todo el proceso completo, pero en la mía se queda a medias con ese mensaje.

No sé que pueda ser dado que implementé el mismo código. Mi sitio web está desarrollado en Wordpress.

Este es el HTML del correo que tengo en mi página

Código HTML:
Ver original
  1. <form id="contactForm" action="http://www.alvarols.com/wp-content/themes/alvarols2/processForm.php" method="post">
  2.  
  3.   <h2>Contáctate con nosotros...</h2>
  4.  
  5.   <ul>
  6.  
  7.     <li>
  8.       <label for="senderName"><p>Nombre</p></label>
  9.       <input type="text" name="senderName" id="senderName" placeholder="Escribe tu nombre" required="required" maxlength="40" />
  10.     </li>
  11.  
  12.     <li>
  13.       <label for="senderEmail"><p>E-mail</p></label>
  14.       <input type="email" name="senderEmail" id="senderEmail" placeholder="Escribe tu correo electrónico" required="required" maxlength="50" />
  15.     </li>
  16.  
  17.     <li>
  18.       <label for="message" style="padding-top: .5em;"><p>Mensaje</p></label>
  19.       <textarea name="message" id="message" placeholder="Escribe tu mensaje" required="required" cols="80" rows="10" maxlength="10000"></textarea>
  20.     </li>
  21.  
  22.   </ul>
  23.  
  24.   <div id="formButtons">
  25.     <input type="submit" id="sendMessage" name="sendMessage" value="Enviar correo" />
  26.     <input type="button" id="cancel" name="cancel" value="Cancelar" />
  27.   </div>
  28.  
  29. </form>

Este es el PHP


Código PHP:
Ver original
  1. <?php
  2.  
  3. // Define some constants
  4. define( "RECIPIENT_NAME", "www.alvarols.com" );
  5. define( "RECIPIENT_EMAIL", "[email protected]" );
  6. define( "EMAIL_SUBJECT", "Mensaje de un cliente" );
  7.  
  8. // Read the form values
  9. $success = false;
  10. $senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";
  11. $senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
  12. $message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
  13.  
  14. // If all values exist, send the email
  15. if ( $senderName && $senderEmail && $message ) {
  16.   $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  17.   $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  18.   $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
  19. }
  20.  
  21. // Return an appropriate response to the browser
  22. if ( isset($_GET["ajax"]) ) {
  23.   echo $success ? "success" : "error";
  24. } else {
  25. ?>
  26. <html>
  27.   <head>
  28.     <title>Gracias!</title>
  29.   </head>
  30.   <body>
  31.   <?php if ( $success ) echo "<p>Gracias por mandarnos tu mensaje, te responderemos a la brevedad.</p>" ?>
  32.   <?php if ( !$success ) echo "<p>Hubo un problema al mandar el mensaje, vuelve a intentar.</p>" ?>
  33.   <p>Click your browser's Back button to return to the page.</p>
  34.   </body>
  35. </html>
  36. <?php
  37. }
  38. ?>

Y el Ajax es este:

Código Javascript:
Ver original
  1. var messageDelay = 2000;  // How long to display status messages (in milliseconds)
  2.  
  3. // Init the form once the document is ready
  4. $( init );
  5.  
  6.  
  7. // Initialize the form
  8.  
  9. function init() {
  10.  
  11.   // Hide the form initially.
  12.   // Make submitForm() the form's submit handler.
  13.   // Position the form so it sits in the centre of the browser window.
  14.   $('#contactForm').hide().submit( submitForm ).addClass( 'positioned' );
  15.  
  16.   // When the "Send us an email" link is clicked:
  17.   // 1. Fade the content out
  18.   // 2. Display the form
  19.   // 3. Move focus to the first field
  20.   // 4. Prevent the link being followed
  21.  
  22.   $('a[href="#contactForm"]').click( function() {
  23.     $('#content').fadeTo( 'slow', .2 );
  24.     $('#contactForm').fadeIn( 'slow', function() {
  25.       $('#senderName').focus();
  26.     } )
  27.  
  28.     return false;
  29.   } );
  30.  
  31.   // When the "Cancel" button is clicked, close the form
  32.   $('#cancel').click( function() {
  33.     $('#contactForm').fadeOut();
  34.     $('#content').fadeTo( 'slow', 1 );
  35.   } );  
  36.  
  37.   // When the "Escape" key is pressed, close the form
  38.   $('#contactForm').keydown( function( event ) {
  39.     if ( event.which == 27 ) {
  40.       $('#contactForm').fadeOut();
  41.       $('#content').fadeTo( 'slow', 1 );
  42.     }
  43.   } );
  44.  
  45. }
  46.  
  47.  
  48. // Submit the form via Ajax
  49.  
  50. function submitForm() {
  51.   var contactForm = $(this);
  52.  
  53.   // Are all the fields filled in?
  54.  
  55.   if ( !$('#senderName').val() || !$('#senderEmail').val() || !$('#message').val() ) {
  56.  
  57.     // No; display a warning message and return to the form
  58.     $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
  59.     contactForm.fadeOut().delay(messageDelay).fadeIn();
  60.  
  61.   } else {
  62.  
  63.     // Yes; submit the form to the PHP script via Ajax
  64.  
  65.     $('#sendingMessage').fadeIn();
  66.     contactForm.fadeOut();
  67.  
  68.     $.ajax( {
  69.       url: contactForm.attr( 'action' ) + "?ajax=true",
  70.       type: contactForm.attr( 'method' ),
  71.       data: contactForm.serialize(),
  72.       success: submitFinished
  73.     } );
  74.   }
  75.  
  76.   // Prevent the default form submission occurring
  77.   return false;
  78. }
  79.  
  80.  
  81. // Handle the Ajax response
  82.  
  83. function submitFinished( response ) {
  84.   response = $.trim( response );
  85.   $('#sendingMessage').fadeOut();
  86.  
  87.   if ( response == "success" ) {
  88.  
  89.     // Form submitted successfully:
  90.     // 1. Display the success message
  91.     // 2. Clear the form fields
  92.     // 3. Fade the content back in
  93.  
  94.     $('#successMessage').fadeIn().delay(messageDelay).fadeOut();
  95.     $('#senderName').val( "" );
  96.     $('#senderEmail').val( "" );
  97.     $('#message').val( "" );
  98.  
  99.     $('#content').delay(messageDelay+500).fadeTo( 'slow', 1 );
  100.  
  101.   } else {
  102.  
  103.     // Form submission failed: Display the failure message,
  104.     // then redisplay the form
  105.     $('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
  106.     $('#contactForm').delay(messageDelay+500).fadeIn();
  107.   }
  108. }// JavaScript Document

Etiquetas: ajax, contacto, formulario, html, input, php
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 11:52.