You are here: Re: Saving a form to the server « PHP Programming Language « IT news, forums, messages
Re: Saving a form to the server

Posted by Mike P2 on 05/13/07 17:34

On May 13, 9:48 am, Dave Kelly <daveeke...@earthlink.net> wrote:
> There has to be a name for what I want to do and I don't know what words
> to google for.
>
> I have a form here:http://www.texasflyfishers.org/firstpage.htm
>
> I want to submit it to the server and have it saved:
> 1. In a directory by the guides name which would be the variable
> 'describe11'. Create this directory if it does not already exist.
> 2. Create a special file name from the date and time submitted.
>
> This form will be filled out by a bunch that are not very computer
> literate, so I must automate everything for them.
>
> I've been searching/googling for 3 weeks now with little to no success.
> Can anyone help?
>
> TIA
> Dave
> --
> A little rum in the morning coffee. Just to clear the cobwebs, ya know.

At the moment, that website is down. Based on your description, I
think I can guess at what you need though.

When you submit a form to a PHP page, that PHP script can access the
data that's in the form through the $_REQUEST superglobal array. You
can be more specific and use either $_GET or $_POST, which correspond
to the method attribute on the HTML form tag. You most likely want to
use the POST method to keep the url looking clean. You can just use
$_REQUEST no matter what method you use. If you change you mind later,
you can just change the HTML form tag.

So here's an example of how to use your superglobals:

<?php // this is whatever.php
if( isset( $_REQUEST['submitBtn'] ) )
{
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];

//do something with those variables...
}
?>
<html><head><title>myform</title></head><body>
<form id="myForm" method="post" action="whatever.php">
<label for="name">Name</label><br />
<input type="text" id="name" /><br /><br />

<label for="email">Email</label><br />
<input type="text" id="email" /><br /><br />

<input type="submit" id="submitBtn" value="submit" />
</form>
</body></html>

Now you have your data, and what you do next depends on what kind of
data it is. You should probably save it into a database. Databases are
designed to make it easier to store and retrieve data. If you don't
have access to a database, then it's ok to just use flat files as you
mentioned. The data is easier to store than retrieve with flat files.

Here's what you can do with those variables in the body of the
conditional (to save in files):

<?php
$baseDir = 'stuff';
if( !is_dir( "$baseDir/$describe11" ) )
mkdir( "$baseDir/$describe11" );
$filename = time()."|{$_REQUEST['name']}.dat";
file_put_contents( "$baseDir/$describe11/$fileName",
serialize( $_REQUEST ) );
?>

Let's assume you made $_REQUEST['name'] and $describe11 safe for the
file system. The above will put the form data into a file in /stuff/
<guide>/<time>|<name>.dat.

Here's another simple example to retrieve the data with another file:

<?php // read.php
$baseDir = 'stuff';
$guide = isset( $_REQUEST['guide'] )
? $_REQUEST['guide']
: 'defaultguide';

$stuff = dir( "$baseDir/$guide" );
while( false !== ( $item = $stuff->read() ) )
{
if( !preg_match( '#([0-9]+)\|(.*)\.dat#i', $item, $info ) )
continue;

$data = unserialize( file_get_contents( "$baseDir/$guide/$item" ) );

echo "Time: {$info[1]}<br />Name: {$info[2]}<br />Data:<br />",
str_replace( "\n", '<br />', str_replace( ' ', '&nbsp;',
$data ) ),
'<br /><br />';
}
?>

You would be able to list all submissions associated with a guide by
adding ?guide=<guidename> to the end of the url. You should make that
variable safe for the file system before using it, though.

-Mike PII

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация