Reputation: 2070
I am trying to set up a really simple coming-soon page with a contact form. I have uploaded everything here (click contact us now button).
Now, when I fill in the information and click send, it will give me an error message. I can't see anything wrong and have error reporting set to on (maybe on the wrong place), here's the php code:
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
if (isset($_REQUEST['name'],$_REQUEST['email'])) {
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$to = 'editedforstackoverflow';
$subject = 'Contact Request From Website';
$headers = "From: ".$name." <".$email."> \r\n";
$send_email = mail($to,$subject,$message,$headers);
echo ($send_email) ? 'success' : 'error';
}
?>
And here the js part:
$('#ajax-contact').on('submit', function() {
// Add text 'loading...' right after clicking on the submit button.
$('.output_message').text('Loading...');
var form = $(this);
$.ajax({
url: form.attr('action'),
method: form.attr('method'),
data: form.serialize(),
success: function(result) {
if (result == 'success') {
$('.output_message').text('Nachricht erfolgreich geschickt!');
} else {
$('.output_message').text('Da ist leider ein Fehler unterlaufen!');
}
}
});
// Prevents default submission of the form after clicking on the submit button.
return false;
});
I can't really see what error I made, any help would be appreciated!
Upvotes: 1
Views: 485
Reputation: 455
your if statement is totally wrong.
you can't separate 2 statements with ,
,it should be separated with ||
or &&
.
The Logical Operators article will get you familiar with these operators.
also, $_REQUEST
is very insecure.its best not to use them.the reason for not using them is explained Here.
Upvotes: 1
Reputation: 41885
Looking at your form in your site, it seems your <input>
tags are missing those name
attribute on the name
and email
fields:
They need to have that name="name"
and name="email"
so that .serialize()
could get those values and bring them into your request. So that in turn in PHP, they'll be inside your $_REQUEST
Here's a demo on what I mean on those missing name=""
. I've explicitly omit field 3 so that you can see.
http://codepad.viper-7.com/rTTPuq
So the fix is. Just add name="name"
and name="email"
on those <input>
tags.
Actual note in the manual:
Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute.
Upvotes: 2