0

Whenever i use a contact form on my website, everything is working fine except message. It is returning [object HTMLSpanElement]

Here is my code of mail.js i have created separate files because i am also using this file for error handling.

function submit_form()
{   
var letters = /^[A-Za-z]+$/;
var message     =   document.getElementById('subject').value;

$.ajax({
type:'post',
url:'enquiry.php',      
data:'name='+name+'&email='+email+'&contactno='+contactno+'&message='+message1,
success:function(data)
{
if(data=="1")
{
//document.getElementById('success').innerHTML="Your message has been sent successfully.";
$('#success').html("Your message has been sent successfully.")

and here is the code of enquiry.php

$name           =   $_POST['name'];
$email          =   $_POST['email'];
$contactno      =   $_POST['contactno'];
$message1       =   $_POST['message'];
$to         =   "emailaddress@email.com";
$subject    =   "Enquiry";
$from       =   "emailaddress@email.com";   
$message = "
<html>
<head>
<title>ENQUIRY</title>
</head>
<body>
<h4>ENQUIRY</h4>
<b>Full Name</b>: $name<br />
<b>Email Id</b>: $email<br /> 
<b>Contact</b>: $contactno <br />
<b>Message </b>: $message1<br />
</body>
</html>";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;

if(mail($to,$subject,$message,$headers))
{
    echo 1;     
}

And here is the html for that

<input type="text" name="contactno" maxlength="10" pattern="[1-9]{1}[0-9]{9}" id="contactno" placeholder="Enter 10 digit Contact Number" class="footer-form1" value="" onchange="removeerr();" required><br>
<span id="contact1"></span>                 

<textarea id="subject" name="message" id="subject" placeholder="Write your message here.." class="footer-massage" value="" onchange="removeerr();" required></textarea>
<span id="message1"></span>
<span id="success"></span>
  • Show us more of your JS code and the related HTML. More than likely, `message1` is an HTML element, not its text. – Mikey May 22 '18 at 18:53

1 Answers1

1

When you send the data with:

data:'name='+name+'&email='+email+'&contactno='+contactno+'&message='+message1,

message1 is a reference to the HTML span element with that id. Your message appears to be inside of the textarea element with an id of subject and you are correctly getting that data and storing it, but you are calling your variable message (not message1), so the line that sends the data should be:

data:'name='+name+'&email='+email+'&contactno='+contactno+'&message='+message,
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71