Jump to content

Need help creating a server side script to save input from html forms


p0ppins

Recommended Posts

Hi,

I have made a basic html site and it has two forms on it. I have the forms linking to a file called process.php.

Basically i need to know what code to put in the php file in order for the forms to save whatever is entered in them to a txt file on my server or computer.

Or any other easier way to do the same thing, save the content of two forms on my site.

Hope i posted this in the right forum subject.

Thanks very much for anyone that helps.

I am no expert.

Have you considered using a database.?

 

If you need a text file though I use this code to store as a text file. (create the file first) In this one I am storing just a single number for use as a switch in a program. I read the number, use it, and then change it for next time

 

This bit reads the text file

$imageDisplay = fopen("last_idx_image.txt","r"); $imageNumber = fgets($imageDisplay); fclose($imageDisplay);

 

This bit writes data to the text file;

$imageDisplay = fopen("last_idx_image.txt","w"); fwrite($imageDisplay, $imageNumber,1000); fclose($imageDisplay);

 

hope it helps

 

 

 

Are you having trouble capturing the submitted form data or do you need help on how to store the captured data?

 

Umm both i think? lol.

Sorry i know how to create the html forms. But have no idea how to create the php script to save the entered data into a txt file.

Thanks

In short, to stick it into a text file you would need to:

 

<?php
$arr[] = "\n"; //start the data off with a new line.
foreach($_POST as $key => $value) { //cycle through the post array, and assign the keys and values.
$arr[] = $key . '=' . $value; //making the keys and values into a string, assigning them to an array.
}
$arr[] = '-----------------------------------------------';  //line of separation at the end of current data.

$file = 'testing.txt'; //file name you wish to save to.
file_put_contents($file,implode("\n",$arr),FILE_APPEND); //append the data (after joining the data with new lines) to the end of the file.

//Now let's show our data.
echo '<pre>' .  file_get_contents($file) .  '</pre>';
?>

Hey,

 

take a look at how I write to a file.  The post is about a file upload, but it also writes to the file.  You might learn something.  Hope it helps.

 

http://www.jotorres.com/en/2011/10/simple-file-upload-in-php/

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.