Blue Cyclone
Blue Cyclone

Reputation: 103

How do I save form data to a text file?

I know this question has been asked before, but I'm trying to diagnose why this isn't working. I want to write form data into a .txt file using a post request. I don't know much PHP at all, as this is a quick program I'm patching together. Here's the code:
Javascript:

function submitdata() {
document.querySelector("#val").innerHTML = input.value + ": " + input1.value;
document.querySelector("#submitform").submit(); }

HTML:

<form style="display: none;" method="POST" name="myform" id="submitform">
  <input id="val" name="val">
</form>

PHP:

<?php           
if(isset($_POST['val']))
{
$data=$_POST['val'];
$fp = fopen('data.txt', 'a+');
fwrite($fp, $data);
fclose($fp);
}
?>

Upvotes: 1

Views: 531

Answers (1)

smartdroid
smartdroid

Reputation: 311

How does your browser know where to send the form data? You need to specify the php file name in form action attribute.

Edit- added relevant point from comment below.

I have pointed out the obvious error based on what you have provided, but it might not be the only one. Other error is you are using innerHTML on an input element. So this may not set the value for #val (some browsers may set the value, some may not).

Upvotes: 1

Related Questions