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
<form id="member_form" name="member_form" method="post" action=""> <p> <label>First Name: </label> <input type="text" name="mf_first_name" id="mf_first_name" /> </p> <p> <label>Last Name: </label> <input type="text" name="mf_last_name" id="mf_last_name" /> </p> <p> <label>Email Address: </label> <input type="text" name="mf_email" id="mf_email" /> </p> <p> <label>Physical Address: </label> <input type="text" name="mf_address" id="mf_address" /> </p> <p> <label>City: </label> <input type="text" name="mf_city" id="mf_city" /> </p> <p> <label>State: </label> <input type="text" name="mf_state" id="mf_state" /> </p> <p> <label>Country: </label> <input type="text" name="mf_country" id="mf_country" /> </p> <p> <label>Mobile Phone: </label> <input type="text" name="mf_mphone" id="mf_mphone" /> </p> <p> <input type="submit" name="register_btn" id="register_btn" value="Register" /> </p> </form>
[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.
<?php /*Some code before now*/ //Use mysql_real_escape_string() if a connection to the DB is already initialized $mf_first_name = mysql_escape_string($_POST['mf_first_name']); $mf_last_name = mysql_escape_string($_POST['mf_last_name']); $mf_email = mysql_escape_string($_POST['mf_email']); $mf_address = mysql_escape_string($_POST['mf_address']); $mf_city = mysql_escape_string($_POST['mf_city']); $mf_state = mysql_escape_string($_POST['mf_state']); $mf_country = mysql_escape_string($_POST['mf_country']); $mf_mphone = mysql_escape_string($_POST['mf_mphone']); /*Some codes afterward*/ ?>
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:
<?php /*Some code before now*/ foreach($_POST as $inputKey=>$inputValue): if((strstr($inputKey,'mf_') === true)): $inputKey = mysql_real_escape_string($inputValue); endif; endforeach; /*Some codes afterward*/ ?>
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.
<?php /*Some code before now*/ $_SESSION['mf_first_name'] = $mf_first_name; $_SESSION['mf_last_name'] = $mf_last_name; $_SESSION['mf_email'] = $mf_email; $_SESSION['mf_address'] = $mf_address; $_SESSION['mf_city'] = $mf_city; $_SESSION['mf_state'] = $mf_state; $_SESSION['mf_country'] = $mf_country; $_SESSION['mf_mphone'] = $mf_mphone; /*Some codes afterward*/ ?>
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:
<!---Some code before--> <p> <label>First Name: </label> <input type="text" name="mf_first_name" id="mf_first_name" value="<?php if(isset($_SESSION['mf_first_name'])){ echo stripslashes($_SESSION['mf_first_name']); unset($_SESSION['mf_first_name']); } ?>" /> </p> <p> <label>Last Name: </label> <input type="text" name="mf_last_name" id="mf_last_name" value="<?php if(isset($_SESSION['mf_last_name'])){ echo stripslashes($_SESSION['mf_last_name']); unset($_SESSION['mf_last_name']); } ?>" /> </p> <p> <!--Some codes after-->
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:
<?php /*Some code before now*/ foreach($_POST as $postInputKey=>$postInputValue): //Check if variable of a variable isset; if true, then work with it //this will discard any POST value with the variable not set if(isset($$postInputKey))://A good example; if set $mf_first_name, i.e if this variable has been created, work with it //Store in a session Array, $_SESSION['_mf']. $_SESSION['_mf'][$postInputKey] = $_POST[$postInputKey]; //or = $postInputValue endif; endforeach; /*Some codes afterward*/ ?>
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:
<?php function stripZlashes($string) { //This function is to strip slashes for either array or a String if (!is_array($string)) return stripslashes($string); $nvar = array(); foreach ($string as $key => $value) $nvar[stripslashes($key)] = stripZlashes($value); return $nvar; } if(!empty($_SESSION['_mf'])): //Strip all possible back slashes stripZlashes($_SESSION['_mf']); endif; ?> <p> <label>First Name: </label> <input type="text" name="mf_first_name" id="mf_first_name" value="<?php echo (!empty($_SESSION['_mf']['mf_first_name'])?$_SESSION['_mf']['mf_first_name']:'') ?>" /> </p> <p> <label>Last Name: </label> <input type="text" name="mf_last_name" id="mf_last_name" value="<?php echo (!empty($_SESSION['_mf']['mf_last_name'])?$_SESSION['_mf']['mf_last_name']:'') ?>" /> </p> <?php if(!empty($_SESSION['_mf'])): unset($_SESSION['_mf']); endif; ?>
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.
Great
These are wonderful posts. It helps.
Shina.
Good Thought..