Contact Form Issue

I am having more trouble with another form. I have generally followed every thing from the last one I created, but I am have problems on receipt of the email.
I am reeving the email, with the header, but I am not getting the subject, email nore message displayed. Can i have some extra eyes to see what I have missed. The form works fine too, it submits and everything. I am just getting an empty email listing;
Name:
Email:
Subject:
Message:

But none of the filled out information.


<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="PHP/sendemail.php" role="form">
                            <div class="row">
                              <div class="col-sm-6">
                                    <div class="form-group">
                                        <input type="text" id="name" 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" id="email" class="form-control" required="required" placeholder="Email address">
                                  </div>
                                </div>
                                <div class="col-sm-6 col-lg-12 col-md-12">
                                    <div class="form-group">
                                        <input type="text" name="subject" id="subject" class="form-control" required="required" placeholder="Subject">
                                  </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>
                    </div><!--/.col-sm-6-->

PHP;

<?php
	
	$status = array(
		'message'=>'Email sent!'
	);
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $email_to = 'website@mydomain';
    $name = ($_POST['name']); 
    $email = ($_POST['email']); 
    $subject = ($_POST['subject']); 
    $message = ($_POST['message']); 
    $headers = 'From: Website';
	
    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    if (mail($email_to, $subject, $body, $headers));

    echo json_encode($status);
}

It could be limit on request size in your PHP settings. And anyway show us dump of your POST…

var_dump($_POST);

I am not sure on the dump log, or how to actually do it, but i looked into it further after reading your limit request possibility and I have found this bit of code in a .js attachment. I blocked it out and it worked fine;

	
	var form = $('.contact-form');
	form.submit(function () {
	this = $(this);
		$.post($(this).attr('action'), function(data) {
		$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
	},'json');
	return false;
	});

Would you know why this would stop it displaying properly, as this to me just displays the ‘Email Sent’ message at the top of the form?

I don’t know javascript, but in the php that outputs the json:-

…shouln’t the echo be in braces from the if condition?

    if (mail($email_to, $subject, $body, $headers)){

        echo json_encode($status);
    }

So you only get the sent message if true.
Though you could use the if to set the staus and echo it either way.

  1. Why use you $(this) after this line?
this = $(this);
  1. What means $this?

  2. You send no request in $.post(!!!) Clear you have an empty request.

Braces aren’t necessary if there’s only one statement to be executed.

But won’t it echo the staus (‘Email sent!’) regardless if whether the mail function returns true or false?

Yes, because the if line has a semi-colon on the end. If it didn’t, it’d be correct. It might be an idea for the code to display something if there was a problem with the message being sent, too.

It’s probably also about time for someone to suggest using something like PHPMailer rather than the in-built mail() function to send the email more reliably.

1 Like

That’s what I thought. It needs either braces or the semicolon removing.
Though I’m in the habit of using braces as a matter of course, it’s much clearer that the code belongs to the condition.

Yes, that’s what I meant about setting the status text in the condition, having already set a default, then echoing outside of the condition.

Did think about mentioning that too.

Ha! I didn’t spot the semicolon.

And me. And it saves remembering to add them if you need to add something later.

Hi Guys, thank you for your replys. I will have a further look and test this evening on what you have being saying.

With regards to the PHPMailer and a condition to display if there was a problem with the message. I am not sure with these as I am not confident in changing the coding incase it causes further issues.

I have further tested it and again it all works fine without the JS script.

I am not sure how to do this, but is there away to echo the ‘email sent’ just above the html form, without turning the html into a php file?

Also if i create conditions based on the point;

How would i echo that in the html form?

Also from;

I thought this is what i’m using as it is linked through my form as an ‘action’?

Sorry, but I am still very new to php and I struggle to learn through reading, i’m more a particle learner, and it helps me better with guidance too. I appreciate all of you that are helping me with this. Thank you

No, this is the line of your code that sends the email:

    if (mail($email_to, $subject, $body, $headers));

The name you give in the “action” parameter of your form declaration is just the name of the PHP code you call when the “submit” button is pressed.

The JavaScript code that you’ve currently removed would do that - where it does this line:

		$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();

it shows that message that comes back from your PHP code, when you do this:

echo json_encode($status);

The only problem is that you set that to the same value regardless, because your line to the mail() function is terminated with a semi-colon.

So as @SamA74 said above, you need something like:

 if (mail($email_to, $subject, $body, $headers)){

        echo json_encode($status);
    }

and then below it, add an else clause to display something else if mail() returned false.

else {
  // do something here to return a JSON-encoded message
}

That won’t work, though, if you don’t put the JavaScript back in your code. If you intend to leave it out, you might just need to perhaps set a session variable at the end of the form-processing code, and when it redirects to your original form or a status page, display the session variable if it exists.

Thank you.

Now the below code works fine;

<?php
	
	
	$status = array(
		'message'=>'Email sent!'
	);
	
		
if($_SERVER["REQUEST_METHOD"] == "POST"){
	$email_to = 'kevin@kjwtechsolutions.co.uk';
    $name = ($_POST['name']); 
    $email = ($_POST['email']); 
    $subject = ($_POST['subject']); 
    $message = ($_POST['message']); 
	$headers = 'From: KJW Technical Solutions Website';
	
    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    if (mail($email_to, $subject, $body, $headers))
	{

    echo json_encode($status); 
	}
}

But when the message is sent, it then displays the echo on a separate blank page that just states message: Email Sent!

But if i add the js code it does the same thing but send a blank email. Now if I add the following line to the top of the php;

header('Content-type: application/json');

This then displays the ‘Email sent!’ message nicely above the form once the form has been submitted, but again this only send a blank email.

At the moment it is the js code and that line above, causing it to fail, but is needed as you mentioned. This is why I am getting a bit confused.

As @igor_g touched on above, the problem is that the JS code isn’t passing the form field data through to your PHP code, hence you get a blank email. I don’t know enough JS / Ajax to help on that, you could try posting in the JavaScript section of the forum.

If you get the JS working, then the form won’t disappear and the return message, whether good or bad, will display on the form, fade in and out (presumably), and wait for the user to do something else. Without the JS, you’re doing a standard HTML form post which means that the form page will disappear, so your PHP code will have to do something at the end of it, redirect to another page perhaps, or just redirect to the form page and display the success or failure message.

It’d be “nicer” to get the JS working, but it’s best to have both methods dealt with in case any of your users have JavaScript disabled.

Thank you for your response. I will look at seeing if any one on the Javascript page can help like you said.

With fiiting in a failure clause how would i do this within my current form and also do you think I need to add a captcha of some sort?

I think I’d look at that once it’s working, and see how much junk mail you start getting.

Ok thank you,

I currently have it live, just without the JS at the moment as it is generally working. And I have already started to get junk mail…so annoying!!

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