Liam Liam - 3 months ago 8
Javascript Question

AJAX Form submission error

I have a form that I want to validate on submission.

I have the following javascript...

var data = $('.sighting').serialize();
$.ajax({ url: "submit-process.php",
data: data,
type: "POST",
success: function(data){
$(".result").text('Thank you for your assistance.');
},
error: function(data) {
$(".result").text(data);
}
});


And I have the following PHP

<?php
if('date-seen' == '') {
echo 'fail'
} else {
foreach ($_POST as $key => $value)
$message .= "Field ".htmlentities($key)." is ".htmlentities($value)."\r\n";

mail('liam@site.co.uk', 'sghting', $message);
}
?>


If the 'date-seen' input is left blank, I receive [Object, object] instead of 'fail' only then if I fill in this field and try resending It doesnt submit, which im guessing is an error in my PHP?

Any help would be brilliant, thanks.

Answer

'date-seen' == '' tests if two string literals are equal to each other (and they aren't).

You probably want $_POST['date-seen']

You also probably want to make your test:

if ( !empty($_POST['date-seen']) ) {

...instead of comparing a variable that does not exist to an empty string.