javascript – PHP Form Submit (save data into a text file) not working-ThrowExceptions

Exception or error:

So I do have a form on my web page and I would like to save user’s input (email in my case) each time the Submit button is clicked.

HTML:

<form id="email-form" method="post">
    <div class="form-group">
        <input type="email" class="form-control" placeholder="ex: john@gmail.com" name="email" required>
     </div>
     <div class="form-group">
         <input type="submit" class="cta animated-cta" name="submit" value="Continue">
     </div>
</form>

PHP:

<?php
    if(isset($_POST['submit'])) {
        $email = $_POST['email'];
        $file = fopen('list.txt', 'a');
        fwrite($file, $email);
        fclose($file);
    }
?>

The problem is that nothing happens, while the code seems to be correct.
Things I’ve tried: Created myself the list.txt file (thought it doesn’t get created automatically) | Put the script on my host to see if there’s any difference (I coded it locally) | Added a PHP redirect to see if the code is executed once I click the button but isn’t the case.

P.S: I do have a ‘event.preventDefault();’ in my javascript file which is executed once the Submit button is clicked (because I don’t want the page to refresh once I click). Hovewer I’ve removed it but nothing changed.

Thank you in advance!

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *