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??
Code:<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>
Last edited by Orjan; 02-08-2010 at 05:09 AM. Reason: Please use code tags!
the row
shall beCode:fputs(fp,$name);
you simply missed the dollar sign for the variable $fp.Code:fputs($fp,$name);
I got an E_WARNING for that, didn't you?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
Hello mate..
I wrote a tutorial http://forum.codecall.net/php-tutori...files-php.html
Maybe this can help you..![]()
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!
well.. this should do the trick..Code:$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);
}
I tried the code with success on my windows under xampp...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
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.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
That's true.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks