/    Sign up×
Community /Pin to ProfileBookmark

Opening and writing to a file in PHP

Hello,
I am having trouble getting my php page to pickup what a user has typed in a form and then saving it to a file.
Its opening the file fine, its just the getting the info part which does not seem to be working.

here is my code:

[code=php]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>Writing to File</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>

<?php
@extract($_POST);
if(is_writable(‘formdata.csv’))
{
$fp = fopen(‘formdata.csv’,’a’);
$content = “$lastname,$firstname,$emailn”;
fwrite($fp,$content);
fclose($fp);
}
else
{ echo’File is not writable’; }
?>

</head>
<body>

<FORM id=”form1″ name=”form1″ method=”post” action=”#”>
<P>
<LABEL for=”firstname”>First name: </LABEL>
<INPUT type=”text” id=”firstname”><BR>
<LABEL for=”lastname”>Last name: </LABEL>
<INPUT type=”text” id=”lastname”><BR>
<LABEL for=”email”>email: </LABEL>
<INPUT type=”text” id=”email”><BR>
<BR>
<INPUT type=”submit” value=”Send”> <INPUT type=”reset”>
</P>
</FORM>

</body>
</html>

[/code]

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Nov 20.2007 — remove the @ before the extract and see if it throws any errors.
Copy linkTweet thisAlerts:
@talksrauthorNov 20.2007 — remove the @ before the extract and see if it throws any errors.[/QUOTE]

I removed it, no errors come up, you can still enter details into the form but nothing goes into the file.
Copy linkTweet thisAlerts:
@bokehNov 20.2007 — [CODE]<INPUT type="text" id="firstname" [B][COLOR="Red"]name="firstname"[/COLOR][/B] >[/CODE][/QUOTE]Your inputs have no [I]name [/I]attribute.
Copy linkTweet thisAlerts:
@TJ111Nov 20.2007 — Do you have error reporting turned on?

A few things you can try:
[code=php]
<?php
if (!extract($_POST)) {
echo "error extracting POST data";
exit;
}
if(is_writable('formdata.csv'))
{
if (!$fp = fopen('formdata.csv','ab')) { //adding the "b" flag here might help
echo "Error opening File";
exit;
}
$content = "$lastname,$firstname,$emailn";
if (!fwrite($fp,$content)) {
echo "Error writing to file";
exit;
}
fclose($fp);
}
else
{ echo'File is not writable'; }
?> [/code]


Edit: I'm an idiot, I didn't even notice there was no "name" attribute in the form :o
Copy linkTweet thisAlerts:
@talksrauthorNov 20.2007 — Do you have error reporting turned on?

A few things you can try:
[code=php]
<?php
if (!extract($_POST)) {
echo "error extracting POST data";
exit;
}
if(is_writable('formdata.csv'))
{
if (!$fp = fopen('formdata.csv','ab')) { //adding the "b" flag here might help
echo "Error opening File";
exit;
}
$content = "$lastname,$firstname,$emailn";
if (!fwrite($fp,$content)) {
echo "Error writing to file";
exit;
}
fclose($fp);
}
else
{ echo'File is not writable'; }
?> [/code]


Edit: I'm an idiot, I didn't even notice there was no "name" attribute in the form :o[/QUOTE]


Hi, when I run your edited code, the browser comes up saying 'error extracting POST data'
Copy linkTweet thisAlerts:
@TJ111Nov 20.2007 — Did you take bokeh's advice and add the name attribute to your form fields?
Copy linkTweet thisAlerts:
@talksrauthorNov 20.2007 — Yep I have been doing it over the last few mins, should be done in a moment! Thanks?
Copy linkTweet thisAlerts:
@talksrauthorNov 20.2007 — Your inputs have no [I]name [/I]attribute.[/QUOTE]

Thanks for the help, I have given the following a go, I have simplified the form part and have just checked it. It is not erroring, but the url is changing slightly on submission.

This is the code:

[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Writing to File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<?php
extract($_POST)&#894;
if(is_writable('formdata.csv'))
{
$fp = fopen('formdata.csv','ab')&#894;
$content = "$lastname,$firstname,$emailn"&#894;
fwrite($fp,$content)&#894;
fclose($fp)&#894;
}
else
{ echo'File is not writable'&#894; }
?>

</head>
<body>

<form>
<P>
First Name:
<input type="text" name="firstname">
<BR>
Last Name:
<input type="text" name="lastname">
<br>
EMAIL:
<input type="text" name="email">

<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>

</body>
</html>


[/CODE]


Before I click submit, the url is:

http://localhost/w9/index2.php

After I have clicked it, the url is:

http://localhost/w9/index2.php?firstname=rob&lastname=talks&email=talks%40aol.com

It seems to be passing the values into the address, as if its looking for a page using the values I have entered.

Still nothing appearing in the file.
Copy linkTweet thisAlerts:
@TJ111Nov 20.2007 — It's because you haven't defined a default form action, so it is sending them as $_GET variables (in the url query), not $_POST, and your using $_POST in your script. You can either change your script to extract($_GET), but a better approach would be to specify the form action as post.
[code=html]
<form name="contact_info" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<P>
First Name:
<input type="text" name="firstname">
<BR>
Last Name:
<input type="text" name="lastname">
<br>
EMAIL:
<input type="text" name="email">

<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>[/code]
Copy linkTweet thisAlerts:
@talksrauthorNov 20.2007 — It's because you haven't defined a default form action, so it is sending them as $_GET variables (in the url query), not $_POST, and your using $_POST in your script. You can either change your script to extract($_GET), but a better approach would be to specify the form action as post.
[code=html]
<form name="contact_info" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<P>
First Name:
<input type="text" name="firstname">
<BR>
Last Name:
<input type="text" name="lastname">
<br>
EMAIL:
<input type="text" name="email">

<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>[/code]
[/QUOTE]


Thanks now that is working. Only thing is, its inserting the values on line 25 of the file in excel but thats no problem, at least it is doing it.

Thanks guys for all of your help.

Could you just explan to me exactly what this line actually does?

<form name="contact_info" action="<?=$_SERVER['PHP_SELF']?>"
Copy linkTweet thisAlerts:
@talksrauthorNov 20.2007 — Thanks now that is working. Only thing is, its inserting the values on line 25 of the file in excel but thats no problem, at least it is doing it.

Thanks guys for all of your help.

Could you just explan to me exactly what this line actually does?

<form name="contact_info" action="<?=$_SERVER['PHP_SELF']?>"[/QUOTE]



Would it be best for me to think of it as just 'POSTing' the values?
Copy linkTweet thisAlerts:
@TJ111Nov 20.2007 — First off, it's inserting info on line 25 because you tested it 25 times and all those times it wrote nothing, just empty lines. Delete the file and put a new one there for it to use if you'd like.

<i>
</i>&lt;form name="contact_info" action="&lt;?=$_SERVER['PHP_SELF']?&gt; [B]method="post"[/B]&gt;

That method="post" tells the browser that when the forms submits, it will send it as HTTP POST information. This ~basically~ is information sent to the server that is invisible to the user (unless they watch the HTTP headers, which you can do). If you change that to action="get", the browser will append all the form information to the url in the form of a query string.
<i>
</i>http://example.com/test.php?variable=contents&amp;another_var=more_content
[B]?[/B] Query delimeter, anything after this the server/browser knows is not part of the request to the file on the server (test.php), but instead information for that file to use
[B]variable=contents[/B]How the variables are passed in the query. In the case of a form, it would be fieldname=fieldvalue. Multiple variables are seperated by the [B]&amp;[/B] ampersand. All information is also URL encoded, that is, spaces and other special characters are replaced with url safe characters.


The main difference is that with GET, someone could simply enter their own query's into the address bar and change whatever information is being sent, and thus manipulate the data. Post values aren't 100% protected, but generally more secure.

Sorry if that doesn't make alot of sense, my brain isn't firing on all cylinders today.
Copy linkTweet thisAlerts:
@talksrauthorNov 20.2007 — First off, it's inserting info on line 25 because you tested it 25 times and all those times it wrote nothing, just empty lines. Delete the file and put a new one there for it to use if you'd like.

<i>
</i>&lt;form name="contact_info" action="&lt;?=$_SERVER['PHP_SELF']?&gt; [B]method="post"[/B]&gt;

That method="post" tells the browser that when the forms submits, it will send it as HTTP POST information. This ~basically~ is information sent to the server that is invisible to the user (unless they watch the HTTP headers, which you can do). If you change that to action="get", the browser will append all the form information to the url in the form of a query string.
<i>
</i>http://example.com/test.php?variable=contents&amp;another_var=more_content
[B]?[/B] Query delimeter, anything after this the server/browser knows is not part of the request to the file on the server (test.php), but instead information for that file to use
[B]variable=contents[/B]How the variables are passed in the query. In the case of a form, it would be fieldname=fieldvalue. Multiple variables are seperated by the [B]&amp;[/B] ampersand. All information is also URL encoded, that is, spaces and other special characters are replaced with url safe characters.


The main difference is that with GET, someone could simply enter their own query's into the address bar and change whatever information is being sent, and thus manipulate the data. Post values aren't 100% protected, but generally more secure.

Sorry if that doesn't make alot of sense, my brain isn't firing on all cylinders today.[/QUOTE]


Makes perfect sense expecially about the security issues.

Thanks very much for your time in helping me get to grips with this, you have been an excellent help.

Rob ?
×

Success!

Help @talksr 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...