/    Sign up×
Community /Pin to ProfileBookmark

Saving to a text file

Hi,
Can anyone help please.
i am required to have a web page with 3 text boxes.
first name
surname
and email address.
on clicking a submit button i want the text entered into these boxes to save to a text file (specific location). then to automatically link to another webpage.
Your help would be appreciated,

Regards
Shrekpete

to post a comment
HTML

11 Comments(s)

Copy linkTweet thisAlerts:
@MarcDMay 13.2007 — What you want is not possible using HTML alone.

You need to use some server-side programming, such as PHP.

[B]The form[/B]

The form needs to have an action and a method assigned to it.

The action dictates what the form should do after it is submitted (to which page it should go).

The method is the method of sending the data.

GET is the method of sending data through the use of the URL (you probably seen the urls like mypage.php?name=MarcD&age=24), when using POST the data is sent invisible to the user.

[code=html]
<form action="result.php" method="post">
<label for="firstname">First name</label>
<input type="text" id="firstname" name="firstname" /><br />
<label for="surname">Surname</label>
<input type="text" id="surname" name="surname" /><br />
<label for="email">E-mail</label>
<input type="text" id="email" name="email" /><p />
<input type="submit" value="Submit" />
</form>
[/code]


So the form will send the data to result.php using the POST method.

[B]result.php[/B]

This is the
[code=php]
<?php
// create short variable names
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$email = $_POST['email'];

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<html>
<head>
<title>Results from the form</title>
</head>
<body>
<?php
$date = date('H:i, jS F');

echo '<p>Form processed at ';
echo $date;
echo '</p>';

$outputstring = $date . "t Firstname: " . $firstname . "t Surname: " . $surname . "t Email: " . $email . "n";

// open file for appending
@ $fp = fopen("$DOCUMENT_ROOT/formdata.txt", 'ab');

if (!$fp){
echo '<p><strong> Your request could not be processed at this time. '
.'Please try again later.</strong></p></body></html>';
exit;
}

fwrite($fp, $outputstring, strlen($outputstring));
fclose($fp);

echo '<p>Form data written.</p>';
?>
</body>
</html>
[/code]



I hope this helps you out.
Copy linkTweet thisAlerts:
@shrekpeteauthorMay 13.2007 — sorry if this sounds stupid, but how do i get this to work?

i have saved both codes as separate files - html + php.

when i click the submit button the code from the php file is displayed.

the entries are not input into a separate text file.

thanks for the help - please keep it coming

shrekpete
Copy linkTweet thisAlerts:
@ray326May 13.2007 — Then your server doesn't have PHP available.
Copy linkTweet thisAlerts:
@shrekpeteauthorMay 13.2007 — is there anyway to get it available?

or any other way of doing what i required?

regards

Shrekpete
Copy linkTweet thisAlerts:
@felgallMay 13.2007 — The first thing you need to do is find out what server side languages your hosting supports.
Copy linkTweet thisAlerts:
@MarcDMay 13.2007 — 
when i click the submit button the code from the php file is displayed.

the entries are not input into a separate text file.
[/quote]


If you see the php file in plain code, something is wrong.

As indicated by others, it might be the case that your host doesn't support PHP.

What, if any, output are you getting exactly when u submit the form?
Copy linkTweet thisAlerts:
@shrekpeteauthorMay 14.2007 — i can see no evidence of any output from the code - only that the link shows the php code in ie.

shrekpete
Copy linkTweet thisAlerts:
@felgallMay 14.2007 — It would do that if you access the file from o0n your computer unless you have your own web server installed. If you run it from a web server that supports PHP then the script will run instead of being displayed.
Copy linkTweet thisAlerts:
@shrekpeteauthorMay 15.2007 — Thanks again for your help.

I have tried running the code on a free webserver on my machine, Xerver. i think php is enabled. any ideas why it doesnt work now?

or any free webservers with php enabled that might work?+

Can anyone else try it to see if it works please?

How can i amend the directory of the output file?

Regards

Shrekpete
Copy linkTweet thisAlerts:
@masamMay 16.2007 — have we caught any carp or bass lately?

?
Copy linkTweet thisAlerts:
@ray326May 16.2007 — I don't know Xserver. Have you installed PHP? You might want to ditch Xserver and get something like XAMP that installs an Apache, PHP and MySQL bundle.
×

Success!

Help @shrekpete spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...