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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.