Jump to content

Saving data from "form to a file"

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
ankit

ankit

    Newbie

  • Members
  • Pip
  • 2 posts
Hi
I am a Fedora 10 user. I want to create a form that can accept form data and save it to a file. For that, I used the following code, but the file (names.txt) was not created. Can someone please tell me what to do??
<html>
<head></head>
<form method="POST">
<pre>
</br> First Name <input type="text" name="first"/>
</br>  Last Name <input type="text" name="last"/>
</br>       <input type="submit" value="Save the data"/>
</pre>
</form>
<?php
$fp = fopen("./names.txt",'a');
if($_POST['first'] and $_POST['last']){
$name= $_POST['first']." ".$_POST['last']."\n";
print($name);
fputs(fp,$name);
}
fclose($fp);
?>
</html>

Edited by Orjan, 08 February 2010 - 05:09 AM.
Please use code tags!


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
the row

fputs(fp,$name);

shall be

fputs($fp,$name);
you simply missed the dollar sign for the variable $fp.

I got an E_WARNING for that, didn't you?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Guest_Jaan_*

Guest_Jaan_*
  • Guests
Hello mate..
I wrote a tutorial http://forum.codecal...-files-php.html
Maybe this can help you.. :)

#4
ankit

ankit

    Newbie

  • Members
  • Pip
  • 2 posts
I tried both the ways.

First I corrected the mistake of $. But it did not work. So, I tried Jaan's method, but still no file was created.

Actually, I am working over PHP on my desktop and not on any server. Is that the reason for the file not being created.
Please help!

#5
Guest_Jaan_*

Guest_Jaan_*
  • Guests
$name = $_REQUEST['name']." ".$_REQUEST['last'];
$file = "../names.txt";
$open = fopen($file, "a");
if(!fwrite($open, $name){
echo "No names were written to file. Check permissions!";
}else{
fclose($open);
}

well.. this should do the trick..

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
I tried the code with success on my windows under xampp...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Would rather use $_POST instead of $_REQUEST as $_REQUEST variables can as well be created through link ( contains both $_POST and $_GET variables ). However that's just my view on it.

#8
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts

webcodez said:

Would rather use $_POST instead of $_REQUEST as $_REQUEST variables can as well be created through link ( contains both $_POST and $_GET variables ). However that's just my view on it.


Sometimes, you might wanna mix get and post, and sometimes, you want to make your code generic to work with both, as you might reuse it in many places, then $_REQUEST is great.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#9
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
That's true.