Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP mailer with ajax textarea

Featured Replies

Hello all,

 

I have been following a tutorial to get a contact form to send and display a message without refreshing the page here - http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

 

Got it to work fine and integrated it with my website.

 

The problom occurs when I add a textarea, it will validate it fine but will not send the contents in the email.

 

If anyone would be able to shed some light on why this may be that would be amazing.

 

html, js and php code here - they are simple just for testing.

 


<form name="contact" method="post" action="">
   <fieldset>
     <label for="name" id="name_label">Name</label>
     <label class="error" for="name" id="name_error">This field is required.</label>
     <input type="text" name="name" id="name" size="30" value="" class="text-input" />

     <label for="email" id="email_label">Return Email</label>
     <label class="error" for="email" id="email_error">This field is required.</label>
     <input type="text" name="email" id="email" size="30" value="" class="text-input" />

     <label for="phone" id="phone_label">Return Phone</label>
     <label class="error" for="phone" id="phone_error">This field is required.</label>
     <input type="text" name="phone" id="phone" size="30" value="" class="text-input" />


   </fieldset>
	<fieldset>
               <label for="test">Message</label>
	<label class="error" for="test" id="test">This field is required.</label>
               <textarea type="test" id="test" name="test" size="30" value="" class="text-input""></textarea>

	<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</fieldset>	

 </form>

 

$(function() {
 $('.error').hide();
 $('input.text-input').css({backgroundColor:"#FFFFFF"});
 $('input.text-input').focus(function(){
   $(this).css({backgroundColor:"#FFDDAA"});
 });
 $('input.text-input').blur(function(){
   $(this).css({backgroundColor:"#FFFFFF"});
 });

 $(".button").click(function() {
	// validate and process form
	// first hide any error messages
   $('.error').hide();

  var name = $("input#name").val();
	if (name == "") {
     $("label#name_error").show();
     $("input#name").focus();
     return false;
   }
	var email = $("input#email").val();
	if (email == "") {
     $("label#email_error").show();
     $("input#email").focus();
     return false;
   }
	var phone = $("input#phone").val();
	if (phone == "") {
     $("label#phone_error").show();
     $("input#phone").focus();
     return false;
   }
	var test = $("textarea#test").val();
	if (test == "") {
     $("label#message_error").show();
     $("textarea#test").focus();
     return false;
   }

	var dataString = 'name='+ name + '&email=' + test + '&test=' + message + '&phone=' + phone;
	//alert (dataString);return false;

$.ajax({
     type: "POST",
     url: "bin/process.php",
     data: dataString,
     success: function() {
       $('#contact_form').html("<div id='message'></div>");
       $('#message').html("<h2>Contact Form Submitted!</h2>")
       .append("<p>We will be in touch soon.</p>")
       .hide()
       .fadeIn(1500, function() {
         $('#message').append("<img id='checkmark' src='images/check.png' />");
       });
     }
    });
   return false;
});
});
runOnLoad(function(){
 $("input#name").select().focus();
});

 

<?php 

$myemail = 'xxxxxx@gmail.com';//<-----Put Your email address here.

$name = $_POST['name']; 
$number = $_POST['phone']; 
$email = $_POST['email']; 
$test = $_POST['test']; 



$to = $myemail; 
$email_subject = "Quote: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n \n Name: $name \n \n Number: $number \n \n Email: $email \n \n  test: \n $test"; 

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);



?>

 

Many thanks!!

in the javascript find

 

 var dataString = 'name='+ name + '&email=' + test + '&test=' + message + '&phone=' + phone;

 

replace with

 

 var dataString = 'name='+ name + '&email=' + email + '&test=' + test + '&message=' + message + '&phone=' + phone;

  • Author

in the javascript find

 

 var dataString = 'name='+ name + '&email=' + test + '&test=' + message + '&phone=' + phone;

 

replace with

 

 var dataString = 'name='+ name + '&email=' + email + '&test=' + test + '&message=' + message + '&phone=' + phone;

 

Thank you, but still no luck :(

ok, well theres more than one bug here then.

 

just noticed an extra quote in the text area html, on the class attribute

<textarea type="test" id="test" name="test" size="30" value="" class="text-input""></textarea>

 

fixed version:

<textarea type="test" id="test" name="test" size="30" value="" class="text-input"></textarea>

Edited by SniderDK

<?php 

$myemail = 'xxxxxx@gmail.com';//<-----Put Your email address here.

$name = $_POST['name']; 
$number = $_POST['phone']; 
$email = $_POST['email']; 
$test = $_POST['test']; 



       $to = $myemail; 
       $email_subject = "Quote: ".$name;
       $email_body = "You have received a new message. \n \n
       Here are the details:\n \n Name: ".$name." \n \n Number: ".$number." \n \n Email: ".$email." \n \n  test: \n ".$test; 

       $headers = "From: ".$myemail; 
       $headers .= "Reply-To: ".$email_address;

       mail($to,$email_subject,$email_body,$headers);



?>

 

How's this?

Edited by Samus

thats missing the newline chars in the headers, also changing the way php contructs the string wouldnt fix this issue as there is no diffrence in the end result only how php goes about making the string.

 

the OP said the new textarea he added wasnt put into the email sugesting the email sends but the textarea is not in there... which is the asumption i've been going by... meaning the error would be regarding the test varibale actually getting used.

  • Author

Thank you all for the help, it is now working correctly :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.