I'm trying to write this form's input to a new .txt file with php, without redirecting to a new page. This code is written in index.php and I suppose I can't put that as the 'form action= ' as well (sorry, looking back it feels especially stupid). My Javascript knowledge is limited, but I believe this is a matter for .ajax? A yes or no would be great! And if you know a good place to learn what I need to, even better!
<form action="index.php" method="post">
<input type="text" name="email" class="emailSubmitSidebar" placeholder=" Your Email">
<input type="submit" class="submitButton">
</form>
<?php
$fileHandle = fopen('emailList.txt', 'w+')
OR die ("Can't open file\n");
$email=$_POST("email");
$result = fwrite ($fileHandle, $email);
if ($result)
{
print '<script type="text/javascript">';
print 'alert("Email added!")';
print '</script>';
} else {
print '<script type="text/javascript">';
print 'alert("Email not added!")';
print '</script>';
};
fclose($fileHandle);
?>