Jump to content

[SOLVED] Record form entry to text file, possible ?


Pr0digy

Recommended Posts

Hi folks,

 

I have a simple HTML form and i want the site to record what someone puts in there.

Is there a way to have it saved to a text file on the server after someone hits "submit" ?

 

It's just one word that's being entered.

 

Here's the form code:

 

<form action="test.php" method="post" target="TestFrame">

	<strong><br />
	<br />
	<br />
	Enter Namel</strong>
	<input type="text" name="name" id="name" value="" onChange="javascript:this.value=this.value.toUpperCase();"/>
	<input type="submit">
	<br />
    </form>

 

I REALLY want to avoid stuff like databases etc since the word 'database' alone scares me :P

 

Thanks for any help :D

 

Jay

 

You can't avoid them forever! Database's make life SO much easier. But yes, you can write to files with functions like:

 

The old:

fopen()

fwrite()

 

The new:

file_get_contents() (Although it was around back in PHP4)

file_put_contents()

 

Hopefully the manual gives you a bit of a push. :)

 

Good luck. Any problems, just post them here. But try and attempt it yourself.

Hey thanks for the reply, looks promising :D

 

Do you have a site like that that shows an example script or anything working with a form, just 1 line is nice but i wouldn't know where to put it :S If it's a matter of putting it between <?php and ?> tags and changing some path names i'd be fine :P

 

Jay

I don't know how you got there so fast but after google page 34 i gave up  :o

I will look at this after i wake up with fresh rested eyes and start messing around (almost 6:30 AM here), thanks so much for the help so far :)

 

jay

Thanks for the links :D

 

I browsed around on Pixel2Life and found this (hopefully promising) snippet and link...

 

require('log.php');

<?php

function log_action($msg) {
    if ($fp = @fopen("/var/lib/mysql/php_app.log", "a")) {
        fwrite($fp, $msg, strlen($msg));
        fclose($fp);
    }
}
?>

 

And this is a link: http://www.gfx-depot.com/forum/-tutorial-index-t-1040.html

 

I think most of what i need is there but im still lost as how to merge this kind of code with my form and make it work. I have a plain text file on the server called "test.txt".

 

I will keep messing around with this but if someone knows how to get this code to work i might even get this done before the end of the year  :P

 

Jay

Well i think i've gotten as far as i can..

 

<form action="serial.php" method="POST" target="MacSerial2Frame">
	<strong><br />
	<br />
	<br />
	try it</strong>
	<input type="text" name="serial" id="serial" value="" onChange="javascript:this.value=this.value.toUpperCase();"/>
	<input type="submit" value="Get Info">
	<br />
    </form>
<?php
$handle = fopen("textfile.txt","a"); 
$content = $_POST['serial'];
fwrite($handle, "test");
fclose($handle); 
?>

 

This results in 'test' being written in the textfile.

 

Instead of test i want it to write the contents of the form (that's where the $content = $_POST['serial']; comes in) or so i thought... because it doesn't work.

 

I'm stuck, any help greatly apreciated :)

 

Jay

Make it this:

 

<form action="serial.php" method="POST" target="MacSerial2Frame">
      <strong><br />
      <br />
      <br />
      try it</strong>
      <input type="text" name="serial" id="serial" value="" onChange="javascript:this.value=this.value.toUpperCase();"/>
      <input type="submit" name="submit" id="submit" value="Get Info">
      <br />
    </form>
<?php
if(isset($_POST['submit'])){
$handle = fopen("textfile.txt","a"); 
$content = $_POST['serial'];
fwrite($handle, $content);
fclose($handle);
}
?>

<?php
  $saving = $_REQUEST['saving'];
  if ($saving == 1) { 
    $data = $_POST['data'];
$file = "data.txt"; 

    $fp = fopen($file, "w") or die("Couldn't open $file for writing!");
    fwrite($fp, $data) or die("Couldn't write values to file!"); 

    fclose($fp); 
    echo "Saved to $file successfully!";
  }
?>

<form name="form1" method="post" action="form.php?saving=1">
  <textarea name="data" cols="100" rows="10">
  <?php
    $file = "data.txt";
    if (!empty($file)) {  
  $file = file_get_contents("$file");
  echo stripslashes($file);  
}  
  ?>
  </textarea>
  <br>
  <input type="submit" value="Save">
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.