Php form Sends blank email

Hello Everybody,

I am very very new to coding. I have this form on my web page. linked to a sendemail.php its send the message to my email but the message is blank.

Name:

Email:

Subject:

Message:

thats all it sends

PHP Code :

<?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 = 'MyEmailherer@gmail.com';

    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

    echo json_encode($status); 
    die;

Form Code:

<section id="contact-page" class="container">
        <div class="row">
            <div class="col-sm-8">
                <h4>Contact Form</h4>
                <div class="status alert alert-success" style="display: none"></div>
                <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-5">
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="First Name">
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="Last Name">
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="Email address">
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary btn-lg">Send Message</button>
                            </div>
                        </div>
                        <div class="col-sm-7">
                            <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
                        </div>
                    </div>
                </form>
            </div><!--/.col-sm-8-->

Please help me. Dont know much about php. :frowning:

Form Code

<section id="contact-page" class="container">
        <div class="row">
            <div class="col-sm-8">
                <h4>Contact Form</h4>
                <div class="status alert alert-success" style="display: none"></div>
                <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-5">
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="First Name">
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="Last Name">
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" required="required" placeholder="Email address">
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary btn-lg">Send Message</button>
                            </div>
                        </div>
                        <div class="col-sm-7">
                            <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
                        </div>
                    </div>
                </form>
            </div><!--/.col-sm-8-->

Um I think you’ve forgotten to put your code in between backticks (`)

If you do, you get something like this

<table border="1"><tr><td width="50" height="50">Hello World</td></tr></table>

If you don’t, you get blank.

Well i am not sure what exactly are you talking about. Although i appreciate your response. as far as i understand there is nothing wrong with the HTML code because my web page is working pretty smooth and it is also communicating with the PHP file tht is linked to my form but the PHP file is sending an email which is blank. So i think there is something wrong with the PHP coding itself
http://www.luckywheelscarrental.com/contact-us.html check it out. Thank you

@Mohsin_Yaseen no I meant here on Sitepoint. In your posts. We can’t see your code that you posted here.

What you need to do is find the little pencil icon by reply, and add ` at the start and end of each block of your code.

Sorry i am noob. i think i did wht u said please help… :frowning:

You need to add ` not ( and )

Anyway here is your code.

PHP Code :

`<?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 = 'MyEmailherer@gmail.com';

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
echo json_encode($status); )
die; `

Form Code:

<section id="contact-page" class="container"> <div class="row"> <div class="col-sm-8"> <h4>Contact Form</h4> <div class="status alert alert-success" style="display: none"></div> <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-5"> <div class="form-group"> <input type="text" class="form-control" required="required" placeholder="First Name"> </div> <div class="form-group"> <input type="text" class="form-control" required="required" placeholder="Last Name"> </div> <div class="form-group"> <input type="text" class="form-control" required="required" placeholder="Email address"> </div> <div class="form-group"> <button type="submit" class="btn btn-primary btn-lg">Send Message</button> </div> </div> <div class="col-sm-7"> <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea> </div> </div> </form> </div><!--/.col-sm-8-->

Yea i did that thank you. Do you have any solution related to my problem?

I think your problem is that you haven’t named your input fields. PHP has no idea where to get the name, email, subject and message from to add to your email. So what you need to do is like this:

<input type="text" class="form-control" required="required" placeholder="First Name" name="name">
and
<input type="text" class="form-control" required="required" placeholder="Email address" name="email">
Try replacing those two and lmk if it sends anything.

Still Blank :frowning:

Name:

Email:

Subject:

Message:

thts all it show in email

Probably the best idea will be to start again - as your form is missing various essential stuff.

I would recommend googling php form to email and following tutorials there.

Here are two links to set you off.

Thank you. But i already been on both of those links tried them and tested not working for me. But hey i came across another forum where someone has an issue similar to mine and some expert recommended him this answer. which worked for him.

your code isn’t supplying any data in the .post() method. you need to add $(this).serialize() to get all the form fields/values.

untested but should work -
$.post($(this).attr('action'),$(this).serialize(), function(data) {

But now i am confused where to place this line in my code. Sorry i am very new to php

Did you understand that form fields need a name attribute as CalvT pointed out?
Your processing code is looking for these $_POST['Names'], i.e. name, email, subject and message and none are being found because the name attribute is missing. So note, the processing is looking for example $_POST['name'] yet your form doesn’t have an input with name=“name”. You have first name and last name inputs but nether have a name attribute, e.g. name=“FirstName”, name=“LastName” etc. POST subject is nowhere to be found as well so you need to check that both the form and processing code match $_POST['KEY] to attribute name=“KEY”.
Also note that you should not be sending email from a POST email address. Emails should be sent from an email address setup for your domain like info@mydomain.com You can specify a reply to header for the posted email so that when you reply, you are addressing the email to the person requesting info.

@Mohsin_Yaseen,

if you already put attribute “name” to your input fields.
ex:

 <input type="text" class="form-control" required="required" placeholder="First Name" name="fname">

in your sendemail.php

check if fname is set

if(isset($_POST['fname'])){
       $name = $_POST['fname'];
      // echo the value of fname
     
      echo $name.

}

@Drummin @jemz you both making sense to me to some extent but the thing is this is my first time with php coding i would appreciate if you can tell me which line of code i must delete or replace or modify and what should i type where in my sendemail.php thanks

P.S. I didnt write this PHP as i got it with a ready template so i dont really know what exactly is going on in there.

There may be no problem with the PHP code.
Did you also not write the form mark-up?

In any case, the inputs need to have name attribute values that correspond to what the PHP code is looking for in the $_POST array

@Mohsin_Yaseen

replace this starting from the form tag

  <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-5">
                <div class="form-group">
                    <input type="text" class="form-control" required="required" placeholder="First Name" name="name">
                </div>
                
                <div class="form-group">                
                    <input type="text" class="form-control" required="required" placeholder="Last Name" name="lastname">
                </div>
                
                <div class="form-group">
                   <input type="text" class="form-control" required="required" placeholder="Email address" name="email">
                </div>
                
                <div class="form-group">
                     <button type="submit" class="btn btn-primary btn-lg">Send Message</button>
                </div>
        </div>
        
      <div class="col-sm-7">
          <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
      </div>
     </div> <!--//end row -->
</form> <!--//end form -->

by the way is your form submitting ?

you could change your action in the form like this

action=“http://mysite.com/sendemail.php

hope it helps.

Jemz the form is submitting and i am also receiving an email. But the email is blank it only shows:

Name:

Email:

Subject:

Message:

Thats it, it dont show any of the text fields even dont show the senders email address… and i tried your code it still dont work… thanks for your support

You could try printing your POST array in PHP when you hit sendemail.php, just to ensure that it’s all there and in the appropriate spots… hmm…