Faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

30 of 47 people (64%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I save form data to a file, as with perl cgi.pm query->save(FILE) ?

Sep 17th, 2002 11:28
blair leavell, markus dinter,


try this if your php.ini contains register_globals=on
<html>
<body>
<?
  if (isset($_POST["confirm"]))//posted?
  {
	  //php must have write-access in this directory
    $fp=fopen("data.txt","w");//overwrite existing text file
    fwrite($fp,var_export($_POST,TRUE));//write post-data
    fclose($fp);
  }
?>
<form action='<?=$_SERVER["PHP_SELF"]?>' method="post">
name<input type="text" name="name"><br>
city<input type="text" name="city"><br>
telnr a<input type="text" name="telnr[0]"><br>
telnr b<input type="text" name="telnr[1]"><br>
<input type="submit" name="confirm">
</form>
</html>
after execution in data.txt must be something like this:
array (
  'name' => 'mad',
  'city' => 'max',
  'telnr' => 
  array (
    0 => '23523lk5j23',
    1 => '257899',
  ),
  'confirm' => '',
)
OK, I tried this, and the data.txt file it created was null, so I added:
  ini_set("register_globals", "1");
as the first line to override whatever the server had (not my server)
but I still got a null file when I did the submit.
Any idea what could be wrong?
I really like your way of saving all the post data, but I can't use it
unless I find out why it won't run right on my server.
Thanks,
Blair
for older versions of php try this:
<?
	function var_export($a)
	{
		$result = "";
		switch (gettype($a))
		{
  		case "array":
    		reset($a);
    		$result="array(";
    		while (list($k,$v)=each($a))
				{
    		  $result.="$k => ".var_export($v).", ";
				}
    		$result.= ")";
   		break;
		case "string":
  		$result = "'$a'";
	  	break;
  	case "boolean":
			$result=($a)?"true":"false";
  		break;
		default:
  		$result=$a;
	  	break;
		}
		return $result;
	}
  if (isset($HTTP_POST_VARS["confirm"]))//posted?
  {
	  //php must have write-access in this directory
    $fp=fopen("data.txt","w");//overwrite existing text file
    fwrite($fp,var_export($HTTP_POST_VARS));//write post-data
    fclose($fp);
  }
?>
<html>
<body>
<form action='<?=$PHP_SELF?>' method="post">
name<input type="text" name="name"><br>
city<input type="text" name="city"><br>
telnr a<input type="text" name="telnr[0]"><br>
telnr b<input type="text" name="telnr[1]"><br>
<input type="submit" name="confirm">
</form>
</html>
Well, my server seems to have training wheels on it.  I enabled
register_globals in my htaccess file, but I still can't seem to
reference GET or POST variables directly, which is probably why your
code doesn't work for me.  I am just going to use the old syntax:
    fwrite($fp,$_POST['name']);//write post-data
since this works
I will just use pipe delimited key value pairs to store and retrieve the
form data.  Primitive but it works.
Thanks for all your help.
Blair