Categories
10 September
2010
How-to PHP Tutorials
9755 Views

Easy Way of Storing Form Data in Session with PHP

Have you ever wonder or thought of a quick way to store a long form data in PHP session without writing a line of code for each form input? Well, I will show you how to go about it with just few lines of code.

The conventional way of doing this is to create a variable for each input field and create a session to store each.

Let start with a typical example. Let say we have a form code as

[note]Note that there is a conventional naming prefix I used for each input I am working with. The prefix is mf_. You can use/define whatever you want; I used mf_ to represent member_form, which is the form name.[/note]

Now this is the usual way of storing the input value in a variable with PHP.

Imagine you have a long form, that means you will have to manually create a variable for each. Men, that is so tasking! I was once in this kind of a situation so I decided to write a script that will handle all the form input value. I’ve been using this for a long time now.

Firstly, with the same form input I auto-create a variable for each input field that has the prefix mf_ as in:

Wow! that 5 lines of code will handle 5000+ input fields and more. We are not done yet. That is just for handling variables. Let’s proceed to session storage.

The main reason why we store form data in a session is just because we want to save the user the headache of retyping the information again in-case an error pop-up whilst processing the form. It’s always a good practice because internet user, nowadays, are so impatient. There are thousands of alternative service to yours, so you should always save them(users) the bad experience. Most user don’t like long forms; that’s why social networks, nowadays, have 2 to 3 or 4 input fields in there registration forms just for fast recruitment. They always find a way to make users fill it’s easy to submit data by chunking the form, that is, using a step by step method to collect the necessary info. I guess no one like long form, me neither.

The regular process is just the same way with the conventional variable creation.

Now, to echo this session in the form field if the form is not processed, we have to set a value attribute for each input filed we are working with, as in:

That’s some long-tasking code if am to confess. I will, really, find it hard to comply with this method of coding; that was the push I had to create an easy session handling script for forms. The method is just as simple as the one for variables, as in:

Another wow? Maybe not, cause it’s just the same method but this time stored in an session Array. You can now set the value attribute for the input filed this way:

The function stripZlashes() is to strip the session Array. Then, unset the session index at the end of the script. The user will see the input value when the form return but will the form won’t hold the value if user refresh the page after the return since you’ve unset the session at the end of the script.

Actually, I originally wrote this in PHP 5 (OOP) and it was easy to work with. I hope this is understandable as planned. All am trying to do here is to make you see how easy this is to work with.

Let me know what you think by leaving a comment.

Thanks for reading.

Related Posts:




2 thoughts on “Easy Way of Storing Form Data in Session with PHP

  1. Shyna says:

    These are wonderful posts. It helps.
    Shina.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments

Julianich
2016-01-12 17:02:25
i guess you should install KrojamSoft FilesSearch and see if it can help...
godson
2014-10-21 15:25:24
this lesson is wonderful...
vijayan
2014-01-29 12:52:07
the below command worked for me attrib -s -h *.* /S /D thanks a lot...

Popular Posts

Useful Tools For Your Tweets
19 September 2010
How-to, PHP, Tutorials
Useful Tools For Your Tweets
Twitter is growing really fast and attention is getting shifted to tweets every minute. The common phrase I see on most Facebook status update is 'Follow me @blablabla'. The truth is, am not a twitter freak but I found twitter really useful, I mean so useful. I read peoples' tweet everyday and I found it informative and entertaining; so the need to get real-time update arose in me and I decide to find useful tools that can help me stay up-to-tweet.
HowTo: Enable “Administrator” account on Windows 7
11 January 2011
How-to, PHP, Tutorials
HowTo: Enable “Administrator” account on Windows 7
To enable "Administrator" account on windows: Open command prompt(START MENU -> RUN. Type CMD and press enter) and type in this command: net user administrator /active:yes To disable: net user administrator /active:no" >RUN. Type CMD and press enter) and type in this command: net user administrator /active:yes To disable: net user administrator /active:no" >To enable "Administrator" account on windows: Open command prompt(START MENU -> RUN. Type CMD and press enter) and type in this command: net user administrator /active:yes To disable: net user administrator /active:no
HowTo: Restore Files Hidden by Virus on Windows 7
09 January 2011
How-to, PHP, Tutorials
HowTo: Restore Files Hidden by Virus on Windows 7
I recently had a challenge of recovering lots of folders hidden by a virus on a drive. Some virus always hide files and folders in an external drive and create a new folder with an extension like .exe, .lnk and so on just to fake users to a malicious act. The truth is, most anti-virus will detect this virus and remove it, but it doesn't return or restore the files and folders hidden by the virus. This is the 'headache spot'!

ADS