I am working with PHP mail()
and have a contact form which posts
the input fields submit
. All inputs on the html form have a name
attr attached to them so PHP can retrieve their values.
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="email" class="form-control" required="required" placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
</div>
</div>
</div>
</form>
On submit
the form is posted
to sendemail.php
to retrieve the input values and send the email:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'keilcarpenter01@gmail.com';
$body = 'Name: ' . $name . "
" . 'Email: ' . $email . "
" . 'Subject: ' . $subject . "
" . 'Message: ' . $message;
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
As you can see I am attempting to retrieve the forms values by:
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
Which is referencing the correct names attributed to the name of each form input
But its not getting the actual values so when i get an email all the body of the message contains is:
Name:
Email:
Subject:
Message:
And not the actual contents of the feilds.
I have read that PHP does not parse POST when your enctype different from application/x-www-form-urlencoded
or multipart/form-data
but I dont have any enctype
attr on my form
.
result of var_dump($_POST):
array(0) {
}
I cant see where I have gone wrong
can you tell me what this is doing in your php file?
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
Are you submitting the form trough an ajax call.
Is also important to note as @Darren say, remove those '@' from your code, you don't need it. This will suppress errors in the function if any!
Maybe you can try this instead:
$(document).ready(function(){
$('#submit').click(function(e) {
e.preventDefault;
var form = $('form#main-contact-form');
var formAction = form.attr('action');
$.ajax({
url: formAction,
data : form.serialize(),
dataType: "json",
type : 'POST',
})
.done(function(data) {
//here is your returned array
if(data.type == 'success'){
alert(data.message)
}
})
.fail(function() {
console.log("error");
})
})
})
You can get the raw format like this.
$post = json_decode(file_get_contents('php://input'));
And then access like this
$email = $post->email;
So i figured it out it, it turns out that the class .contact-form
eas already tied to a js
function which used ajax to pass data to the php script. At the time it wasnt catching any data from the form hence why $_POST
was recieving no value.
I fixed the error by passing the form values:
var form = $('.contact-form');
form.submit(function (evt) {
$this = $(this);
$.post('sendemail.php',{ name: $('input[name=name]').val() email: $('input[name=email]').val() message: $('input[name=message]').val() }, function(data) {;
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
evt.preventDefault();
return false;
});