PHP form with response on same page

Hi I got some codes on this site for creating a PHP form plus getting the response message on the same page. Everything about the response works great but when I submit the message I only get the failure response, I will place the codes here so maybe someone can point out a mistake.

sendEmail.php:

<?php

/* Configuration */
$subject = 'Contacto Nebraska Web'; // Set email subject line here
$mailto  = 'info@nebraskafilms.com.ar'; // Email address to send the form to
/* END Configuration */

$name     	= $_POST['name'];
$email          = $_POST['email'];
$comments       = $_POST['comments'];
$timestamp 	= date("F jS Y, h:iA.", time());

// HTML for email to send submission details
$body = "
<br>
<p>Message:</p>
<p><b>Name</b>: $name <br>
<b>Email</b>: $email<br>
<p>This form was submitted on <b>$timestamp</b></p>
";

// Success Message
$success = "
<div class=\"row\">
    <div class=\"thankyou\">
        <h3>Submission successful</h3>
        <p>Mensaje recibido, gracias por contactarte con Nebraska Films, te responderemos a la brevedad.</p>
    </div>
</div>
";

$headers = "From: $name <$email> \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>$body</body></html>";

if (mail($mailto, $subject, $message, $headers)) {
    echo "$success"; // success
} else {
    echo 'Hubo un error, por favor intenta nuevamente.'; // failure
}

?>

form:

<form method="post" action="sendEmail.php" id="contactForm">
     <input type="text" placeholder="Name" name="name" />
     <input type="text" placeholder="Email" name="email" />
     <input type="text" placeholder="Message" name="comments" />
     <input type="submit" value="SEND" id="submit" name="contactSubmit" />
</form>

sending.js code:

$('#contactForm').on('submit', function(e) {
    e.preventDefault(); //Prevents default submit
    var form = $(this); 
    var post_url = form.attr('action'); 
    var post_data = form.serialize(); //Serialized the form data for process.php
    $('#loader', form).html('<img src="../img/loader.gif" /> Please Wait...');
    $.ajax({
        type: 'POST',
        url: 'sendEmail.php', // Your form script
        data: post_data,
        success: function(msg) {
            $(form).fadeOut(500, function(){
                form.html(msg).fadeIn();
            });
        }
    });
});

Any help is appreciated!
Thank you

Seems like mail() function isn’t working on your server.
Did you test it alone?

mmm how do I test that alone?

I also have another form that works perfectly and I get the emails but with this one I can’t get the response message on the same page, it takes me to a blank page with unformatted text. Here is the other PHP code:

<?php

// Replace this with your own email address
$siteOwnersEmail = 'info@nebraskafilms.com.ar';


if($_POST) {

   $name = trim(stripslashes($_POST['contactName']));
   $email = trim(stripslashes($_POST['contactEmail']));
   $subject = trim(stripslashes($_POST['contactSubject']));
   $contact_message = trim(stripslashes($_POST['contactMessage']));

   // Check Name
	if (strlen($name) < 2) {
		$error['name'] = "Por favor ingresá tu nombre.";
	}
	// Check Email
	if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
		$error['email'] = "Por favor ingresá un email válido.";
	}
	// Check Message
	if (strlen($contact_message) < 10) {
		$error['message'] = "Por favor ingresá tu mensaje. Debe contener 10 caracteres como mínimo.";
	}
   // Subject
	if ($subject == '') { $subject = "Contact Form Submission"; }


   // Set Message
   $message .= "Email from: " . $name . "<br />";
	$message .= "Email address: " . $email . "<br />";
   $message .= "Message: <br />";
   $message .= $contact_message;
   $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

   // Set From: header
   $from =  $name . " <" . $email . ">";

   // Email Headers
	$headers = "From: " . $from . "\r\n";
	$headers .= "Reply-To: ". $email . "\r\n";
 	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


   if (!$error) {

      ini_set(sendmail_from,'info@nebraskafilms.com.ar'); // for windows server
      $mail = mail($siteOwnersEmail, $subject, $message, $headers);

		if ($mail) { echo "Tu mensaje fue enviado correctamente."; }
      else { echo "Hubo un error. Por favor intente de nuevo."; }
		
	} # end if - no validation error

	else {

		$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
		$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
		$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
		
		echo $response;

	} # end if - there was a validation error

}

?>

Just make mail.php with

<?php mail('your@email.com', 'test', 'test');

and run it to see if letter will be delivered

Ok it’s fixed, mistake was in the submit button!

Now the problem is that the SEND button does not recognize the CSS style.

Here is the form:

       <!-- form -->
       <form action="sendEmail.php" method="post" id="contactForm" name="contactForm">
			<fieldset>

          <div>
				   <label for="contactName">Nombre <span class="required">*</span></label>
				   <input type="text" value="" size="35" id="contactName" name="contactName">
          </div>

          <div>
				   <label for="contactEmail">Email <span class="required">*</span></label>
				   <input type="text" value="" size="35" id="contactEmail" name="contactEmail">
          </div>

          <div>
             <label for="contactMessage">Mensaje <span class="required">*</span></label>
             <textarea cols="50" rows="15" id="contactMessage" name="contactMessage"></textarea>
          </div>

          <div>
			 <input type="submit" class="button" value="Enviar" id="submit" name="contactSubmit" />

             <span id="image-loader">
                <img alt="" src="images/loader.gif">
             </span>
          </div>

			</fieldset>
		   </form>
       <!-- Form End -->

And here the CSS part of the button:

#contact input,
#contact textarea,
#contact select {
    border: none;
	-webkit-border-radius: 0px;
	-moz-border-radius: 0px;
    border-radius: 0px;
	
    -webkit-box-shadow: none;
	-moz-box-shadow: none;
	box-shadow: none;

    width: 95%;
	background: #efefef;
    color: #767676;
    font-size: 16px;
    height: auto;
    padding: 15px;
	margin: 2;
    resize: none;
}

#contact input:focus,
#contact textarea:focus,
#contact select:focus {
		border:1px solid #bdbc60;
	}

Where in your HTML do you have id=“contact” ;)? Did you mean #contactForm?

Thanks Ryan, I replaced all the “contact” id with “contactForm” but the SEND button remains the same. Here is a link to the site with the contact form at the bottom: http://nebraskafilms.com.ar/

What styles do you believe are not working? Post the styles that you think / want to be applied to the submit.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.