STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
tinoda
Forum Commoner
Posts: 33
Joined: Mon Dec 29, 2008 3:32 am

STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by tinoda »

How can i store form user input permanently. I hv tried to come up with the codes below and all they can do is send the input into the required field but after refresh or the close of browser they disappear. I have also used file_get_contents and the other file writing techniques such as sessions, serialize() unserialize(). However these can keep my data input even after refresh but if i close the browser and start again my data is lost. I also hv to acknowledge that I am a newbie in PHP, by the way, i hv also tried using the javascript DOM methods and innerHTML and they all cant store the dynamic variables permanently. here r the codes I hv created: (sorry I asked this question a few days ago but many said I need to clarify myself. if i am attempting an impossibility please let me know)Thank u:

Code: Select all

<form action="session.php" method="post">Name: <input name="username" type="text" /> Age: <input name="age" type="text" /> <input name="submit" type="submit" /></form>
 
//session.php
 
<?php
session_start();
$_SESSION['action']  = 'a';
    $_SESSION['Username']  = $_POST['username'];
$username = $_SESSION['Username'];
    echo $_SESSION['Username'];
 
?>
------------------------------------------------------------------------------
 
 
<html>
<body><form action="table.php" method="POST">
Name: <input type="text" name="name" />
Age: <input type="text" name="age"  />
<input type="submit" name="submit"/>
</form>
 
 
//tabel.php
 
<?php if($_POST['submit']) {
 
$name=$_POST['name'];
$age=$_POST['age'];
 
 echo " <table border='1'>
<tr>
<td>
Welcome
</td>
<td> 
$name 
</td>
</tr>
<tr>
<td>
You are
 </td>
<td>
 $age years old.
</td>
</tr>
</table>
</body>
</html>";
exit;
}
?>
 
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by jaoudestudios »

You could use a cookie. To store the user's id and save that as a text file on the server. But if the user clears their cache then all would be lost!

Why dont you want to use a database, it would be so much easier! That is what it is designed for. :)
tinoda
Forum Commoner
Posts: 33
Joined: Mon Dec 29, 2008 3:32 am

Re: STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by tinoda »

jaoudestudios wrote:You could use a cookie. To store the user's id and save that as a text file on the server. But if the user clears their cache then all would be lost!

Why dont you want to use a database, it would be so much easier! That is what it is designed for. :)
......i cant use a database for now since its a very small project I am working on. Besides I also want the results to immediately show on the same page. if u see the code I have the form result get sent to a table. so i want for example the name to stay on that html table forever.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by onion2k »

Whether or not you should use a database depends on whether or not it's appropriate; it's not dependent on the size of the site. I've got sites that consist of about 5 lines of PHP that have a database. Because they should. You should use a database too.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by omniuni »

I guess there could be times when a database is overkill. They're rare, to be sure, and I can't think of a very practical example, but sometimes you just don't need the overhead. Here's some code that keeps a list of names. It can add and remove names, and they are stored on the server in a serialized array.

Use such techniques at your own risk.

Live Demo Link: http://my.oi-share.com/name_array

Code: Select all

 
<html>
 
<head>
    <title>My Little Array</title>
</head>
 
<body>
 
<h1>My Little Array</h1>
 
<p>This uses PHP to store values in an array. They are saved on the server.</p>
 
<?php
//Check to see if names should be added or deleted, and do so.
if($_GET['add']){
    $namesArray;
    //Read file into namesArray if it is there
    if(is_file('names.array')){
        $namesArray = unserialize(file_get_contents('names.array'));
        }
    //Add the new name on to the array. Each is noted by the timestamp so it has a unique id. Note, however, that this is a weak scheme that will break if there is more than one submission a second.
    $namesArray[] = stripslashes($_GET['add']);
    //re-key array
    $namesArray = array_values($namesArray);
    //write array back to file
    file_put_contents('names.array', serialize($namesArray));
    
}elseif($_GET['delete']){
    $namesArray;
    //Read file into namesArray if it is there
    if(is_file('names.array')){
        $namesArray = unserialize(file_get_contents('names.array'));
        }
    //unset the array element by key
    unset($namesArray[$_GET['key']]);
    //re-key array
    $namesArray = array_values($namesArray);
    //write array back to file
    file_put_contents('names.array', serialize($namesArray));
 
}
 
//Check if the file names.array exists. If it does, loop through it and list the names along with "delete" links. I want to re-read the array in case something was added or deleted above.
if(is_file('names.array')){
    $namesArray = unserialize(file_get_contents('names.array'));
    
    foreach(array_keys($namesArray) as $nameKey){
        $name = $namesArray[$nameKey];
        echo $nameKey.' | '.$name.' &nbsp; &nbsp; <a href="?delete=true&key='.$nameKey.'">X</a><br/>';
        }
    
    }
?>
 
<!-- Draw a form to be used to add a name -->
 
    <form action="?" method="get">
        <input type="text" name="add" /> <input type="submit">
    </form>
 
</body>
 
</html>
 
Just to talk about this a bit...

The approach here is to manage the data in an array. In PHP, arrays are easy to work with, sort, add to and remove values from, and they can become a pretty powerful data structure when you create a multi-dimensional array, or even create an array of arrays. This example is not object-oriented at all, of course, you could make it object oriented. If you were developing any more complex use of this, I would recommend that you at least abstract things into functions so that your code is less cluttered. As for the actual performance of this script? It's certainly fast, fast enough that you could have hundreds of things in your array and not notice any performance problems- but it's a simple array. If you are going to be storing several dozen profiles of information, avoid arrays all-together and go with a database. If you feel your project is too small for a full DB like MySQL, try SQLite as a fully relational alternative.

Good Luck!
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by allspiritseve »

tinoda wrote:......i cant use a database for now since its a very small project I am working on. Besides I also want the results to immediately show on the same page. if u see the code I have the
form result get sent to a table. so i want for example the name to stay on that html table forever.
You can't or you don't want to? There's no reason you can't use a database. I would recommend MySQL or sqlite. There's really no difference time-wise between storing in a database and storing anywhere else, if your site is small. The results can show immediately on the page, and will be in the database until you remove them, so you shouldn't have any problems.
omniuni wrote:I guess there could be times when a database is overkill. They're rare, to be sure, and I can't think of a very practical example, but sometimes you just don't need the overhead
If the site is small, the overhead will be small too... so I guess I don't see how it can ever be overkill.
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by The_Anomaly »

++ on the notion that a database is almost always needed, and there's almost no reason to avoid it.

However, if you're all that desperate to get away from a database and SQL for some crazy reason, then why not just use a text file or something similar? Probably worse overheadwise, but it's not a database.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by jaoudestudios »

Text file is a good idea, but searching would be a pain.

What about a yaml file, xml, json? Searching would still be an issue.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: STORE FORM INPUT WITHOUT DATABASE - IS IT POSSIBLE?????

Post by omniuni »

That's why I use an array when I do such things; PHP has some good built-in ability to handle them, and they are decently easy to search as well.
Post Reply