Sanjib Sinha Posted January 14, 2011 Share Posted January 14, 2011 I got two values from a form in a page by POST method. Now I want to add this values to a text file. The code of form1.htm is like this: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="write1.php" method="post"> <fieldset> <legend>Log In...</legend> <input type="text" name="uname" /><br> <input type="text" name="pass" /><br><br> <input type="submit" name="button" value="Log In" /> </fieldset> </form> </body> </html> And the code of write1.php is like this: <?php function arr_func() { $name = $_POST['uname']; $pass = $_POST['pass']; //trying to catch values in an array $arr[] = array("name"=>"$name", "pass"=>"$pass"); foreach ($arr as $val_arr) { foreach ($val_arr as $key=>$val) { echo "$key - $val<br>"; } echo '<br>'; } } $arr1 = arr_func(); //values are printed, no problem echo $arr1; //trying to change the array into string, is it wrong approach? $var1 = strval($arr1); // set file to write $file = 'student/dump.txt'; // open file $fh = fopen($file, 'w') or die('Could not open file!'); // write that variable into file fwrite($fh, "$var1\n") or die('Could not write to file'); // close file fclose($fh); ?> After log in in form1 I get two values of $name and $pass in write1.php but no values being added to the file mentioned in the path. Quote Link to comment https://forums.phpfreaks.com/topic/224423-can-the-key-and-value-of-an-array-be-added-to-a-file/ Share on other sites More sharing options...
trq Posted January 14, 2011 Share Posted January 14, 2011 Your function doesn't return any data, it simply echos it. This means that $arr1 is empty. Quote Link to comment https://forums.phpfreaks.com/topic/224423-can-the-key-and-value-of-an-array-be-added-to-a-file/#findComment-1159340 Share on other sites More sharing options...
The Little Guy Posted January 14, 2011 Share Posted January 14, 2011 may I ask why your storing a username and password in a text file, and not a database? Quote Link to comment https://forums.phpfreaks.com/topic/224423-can-the-key-and-value-of-an-array-be-added-to-a-file/#findComment-1159379 Share on other sites More sharing options...
Sanjib Sinha Posted January 14, 2011 Author Share Posted January 14, 2011 Thanks thorpe, when I run the code name and pass got two values which were printed. As you advised, I will try return instead of echo inside the function. But the problem is, in my mentioned path, in the dump.txt file the data has been written but only the pass, ie; the value element of array. The first one, ie; name is missing! Why this happens, I can't understand. Thanks The Little Guy, no I did not want to store password in text file. Actually it was just a test. It could be any kind of text data, like address etc. The situation is like this: in a form someone enters data, like name, age, address etc and the values will be stored in a text file in a format like key=>value. Quote Link to comment https://forums.phpfreaks.com/topic/224423-can-the-key-and-value-of-an-array-be-added-to-a-file/#findComment-1159408 Share on other sites More sharing options...
Sanjib Sinha Posted January 15, 2011 Author Share Posted January 15, 2011 Well, my problem has been partially solved. I have added data to a text file like this: <?php $name = "Name : " . $_POST['uname']; $pass = "Pass : " . $_POST['pass']; $data = $name . "\t" . $pass . "\t" . "\n"; file_put_contents("student/dump.txt", $data, FILE_APPEND); ?> I got two text data from a form and to this extent it works fine. Now I want to know whether it is possible to edit, update or delete these fields in that text file(student/dump.txt) where I append those data. Quote Link to comment https://forums.phpfreaks.com/topic/224423-can-the-key-and-value-of-an-array-be-added-to-a-file/#findComment-1159754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.