Reputation: 35
I am working on creating a contact us form for a website. I am using jquery to create an .click() function for an html button. then using ajax to call the email.php code. When I click the button the console displays the following error:
500 (Internal Server Error).
I'm new to ajax and php being used together so I have no idea what to do at this point.
Here is the HTML form:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src = "js/callmail.js"></script>
</head>
<body ng-app = "">
<div class = "container">
<form name = "contact">
<label for = "subject" class = "control-label">Subject:</label>
<input name = "subject" id = "subject" ng-model = "subject" required/>
<span class = "warning" ng-show="contact.subject.$touched && contact.subject.$invalid">You Must Enter a subject!</span>
<span class = "success" ng-show="contact.subject.$touched && contact.subject.$valid">Valid!</span>
<br/>
<label for = "body" class = "control-label">Body:</label>
<input name = "body" id = "body" ng-model = "body" required/>
<span class = "warning" ng-show="contact.body.$touched && contact.body.$invalid">Your email must have a body!</span>
<span class = "success" ng-show="contact.body.$touched && contact.body.$valid">Valid!</span>
<br/>
<label for = "signature" class = "control-label">Return Email:</label>
<input name = "signature" id = "signature" ng-model = "signature" type = "email" required/>
<span class = "warning" ng-show="contact.signature.$touched && contact.signature.$invalid">You must enter a valid return email!</span>
<span class = "success" ng-show="contact.signature.$touched && contact.signature.$valid">Valid!</span>
<br/>
<button ng-disabled = "contact.$invalid" id = "sendmail" type = "submit" class = "btn btn-success">Submit</button>
</form>
</div>
and here is the jQuery/AJAX call to the php code:
$(function() {
$("#sendmail").click(function() {
var data = {
to: "myemail",
subject: $("#signature").val() + ": " + $("#subject").val(),
message: $("#body").val()
}
$.ajax({
type: "POST",
url: "email.php",
data: data,
success: function() {
alert("Message sent");
}
});
console.log(data);
});
});
And finally, here is the php code that is responsible for sending out the email:
<?php
if ($_POST["submit"]) {
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = "From: " . $_POST["name"] . "Reply to: " . $_POST["signature"];
mail("myemail", $subject, $message, $headers))
}
?>
Any help would be greatly appreciated.
Upvotes: 0
Views: 1434
Reputation: 2132
Firstly, you should have a "submit" key in your JavaScript code.
$("#sendmail").click(function() {
var data = {
to: "[email protected]",
subject: $("#signature").val() + ": " + $("#subject").val(),
message: $("#body").val(),
signature: $('#signature').val(),
name: "Your name",
submit: true //You'll receive $_POST['submit'] in email.php
}
$.ajax({
type: "POST",
url: "email.php",
data: data,
success: function() {
alert("Message sent");
}
});
});
I just added some lines in data variable. Do not forget The data received in $_POST array in PHP are the same that you've send in your ajax method In the code you'd posted, $_POST['name'] was undefined.
<?php
if ($_POST["submit"]) {
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = "From: " . $_POST["name"] . "Reply to: " . $_POST["signature"];
//Use the receiver email you've set in your JS
$to = $_POST['to'];
mail($to, $subject, $message, $headers))
}
?>
Then, as said in an other answer, you should try to develop on you own PC and only when your website is finished deploy it onto your server.
On a Debian based OS for instance you can use it:
$ sudo apt-get install apache2 mysql-server php5
This command will install an Apache webserver, a MySQL server, and PHP 5 on your PC. Or, as said in an other answer you can use XAMP that do the same things.
Once you have a website running, you can configure your server to send email. This post explain how to configure the php.ini in order to use your own mail adress. Of course you can use other than Gmail if you want.
If you receive a mail from your own server, that means your code is right. You can deploy it onto the server, and configure it the same way if necessary.
Upvotes: 0
Reputation: 147
The problem with Error 500 is it's not specific. It just means "something went wrong". It's impossible to diagnose much of anything with just an Error 500.
What you need to find out is what caused the Error 500, which would be in your PHP error log, if you know where that is.
The first question I'd ask you is - what environment are you running PHP in? Is it on your computer or is it on a web server with an internet service provider? If you don't run a development environment on your PC I strongly advise you to get something simple like XAMPP installed and develop there. It's not ideal for advanced developers but for learning your way around PHP, it's good. Not least because it writes out proper error logs you can actually access and read :)
The other thing is to look at the PHP documentation which provides some handy information about the mail function:
http://php.net/manual/en/mail.requirements.php
As you can see - mail() expects to be run on a system with access to a sendmail command. If the system you're running php on either doesn't have sendmail or might not have sendmail this could be your problem. However, without an error log to look at I'm making guesses.
So my advice would be to try on a local installation of PHP first. And also with locally running PHP (eg using xampp) you'll have to set up SMTP server details - the server, username and password and security details you use for sending mail. But you will get access to much more comprehensive logs and error information.
Upvotes: 0