0

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.

8
  • 1
    Webserver on Linux? If so, check the file's permission, the any.txt, might need to be 0755 or maybe even 0777 depending on the server's setup. Commented Jan 6, 2016 at 23:59
  • 2
    Where is $anuwoshere defined? 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. Commented Jan 7, 2016 at 0:03
  • Yes, it was permissions on the text file. chmod 777 worked fine! 755 didn't. Thanks very much. That was big help. Commented Jan 7, 2016 at 0:08
  • 3
    777 for text file with passwords? If anything, you should be using 600/644 for this. I hope you're using a really BIG lock for this (htaccess). Why aren't you using a database for this? Text files are so much work to maintain for stuff like this. Commented Jan 7, 2016 at 0:17
  • 1
    You're welcome and stay safe ;-) Commented Jan 7, 2016 at 0:55

2 Answers 2

1

Webserver on Linux? If so, check the file's permission, the any.txt, might need to be 0755 or maybe even 0777 depending on the server's setup. ;)

You do have to consider security, especially if you are on shared hosting. 0777 might make the file readable by other users on the server.

Sign up to request clarification or add additional context in comments.

Comments

1
        $file = fopen("sdf/sdf/file.txt", "a") or die("Error");
        $txt = "asdfasdfasdfasdfadf";
        fwrite($file, $txt);
        fclose($file);
        // Permissions.
        chmod($file, 0775) // unix / linux- 0777 didn't work for me.

My main server requires the zero. Some don't. Make sure that is there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.