Jump to content

Can the key and value of an array be added to a file?


Sanjib Sinha

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.