I have a webpage with a signup form which takes an email and a password. The html calls javascript function which in return calls php code that saves the email to a file on the server. It seems the javascript is working fine but the php code doesn't save to file.
Any suggestions on what's going wrong would be appreciated.
Javascript snippet:
$("#signup-divider").submit(function(e) {
e.preventDefault();
var data = {
email: $("#signup-email").val(),
password: $("#signup-password").val()
};
if ( isValidEmail(data['email']) && (data['password'].length > 1)) {
$.ajax({
type: "POST",
url: "assets/php/subscribe.php",
data: data,
success: function() {
$('.signup-success').fadeIn(1000);
$('.signup-failed').fadeOut(0);
}
});
} else {
$('.signup-failed').fadeIn(1000);
$('.signup-success').fadeOut(500);
}
return false;
});
subscribe.php :
<?php
//This Script only check Email Address and add it to email-list.txt
$email = trim($_POST['email']);
// Email address validation - works with php 5.2+
function is_email_valid($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if ( isset($email) && is_email_valid($email) ) {
file_put_contents("email-list.txt", "$email\r\n", FILE_APPEND);
}
?>
Edit: Please note that this is test code. I plan to replace the text file with email notifications and remove passwords. I'm taking a stock template which I don't understand very well and modifying it.
$anuwosheredefined? Is the PHP executing without any errors? You are trimming the email before checking if it is set so that could cause an Undefined index error and will prevent the script from continuing its execution.