We help IT Professionals succeed at work.

how to save variables passed from an html form in php

sara_bellum asked
Medium Priority
Last Modified: 2012-05-06
I have an html form with select option values for days of the week and hours of the day that I pass to a php script that has a nested array and a form to submit changes to the array (excerpt copied below).  I need to save the form values that are passed to the php script in order to change the array but I don't understand how it works.

I'm much less interested in the perfect answer (which would probably be a complete rewrite) than I am in understanding how this works, so  I'd like to keep the 2 files that I have and, in the end, incorporate the change submitted by the 2d form (in the php file) into the array.

Since both the nested array and the change form are in the php file, I'm thinking that I need another file with 777 permissions that the script would write to: it would have to copy the entire array with the change and overwrite the file with each new change.  Of course, I don't know how that would work either :(
The nested array:
$daily_schedule = array("Sunday" => array("9am" => "eat breakfast",
                                      "10am" => "go to church",
                                      "11am" => "drink coffee",
                                      "12pm" => "eat lunch",
                                      "1pm"  => "read paper",
                                      "2pm"  => "work out",
                                      "3pm"  => "do laundry",
                                      "4pm"  => "do math homework",
                                      "5pm"  => "cook dinner"),
                 "Monday" => array("9am" => "go to work",
                                      "10am" => "start work", ...
# The form values passed from daily.html (skipped copy of that form, it works):
$days = $_POST["weekday"];
$hours = $_POST["hourly"];
#Call a function with options to display the whole week, one day, or one hour of one day 
function pickAll($daily_schedule) {
} else if(($days != "Choose") && ($hours != "Choose")) {
 foreach ($daily_schedule as $week_days => $hours_array) {
   if($days == $week_days) {
     $days = $save_day;
     echo "<br/>Day from array keys: ".$week_days;
     echo "<br/>Saved variable, day: ".$save_day; //this fails
     while (list($key,$value) = each($hours_array)) {
       if($hours == $key) {
       echo "<br/>Hour from array keys: ".$key;
       echo "<br/>Task from array values: ".$value;
       $key = $save_hour;
       echo "<br/>Saved variable, hours: ".$save_hour; //this fails
       } else {
       echo "";
# The change form (variables defined but omitted):
<form id="change_schedule" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"></td></tr>
<tr><td align="center" colspan=2>Note: this
change will only be effective for the above day and hour</td></tr>
<tr><td align="center" colspan=2>
<input type="text" name="schedule" value="" />
<tr><td align="center" colspan=2>
<input type="submit" value="Change"/>
<p>Return to <a href="daily_schedule.html">form</a></p>

Open in new window

Watch Question

Brief overview of how data passing works in PHP / web servers:

Each time a page loads, the request is handled independantly of other page loads. When the page is loading, the server executes the PHP code, and whatever HTML is returned from the PHP program is what is returned back to the user's web browse.r

A user may send data to the server by submitting a form. When a form is submitted, the receiving page's PHP code can access the submitted content using the $_POST array. The data is only available for one page request, which occurs when the user clicks the "Submit" button on the form.

Information may be preserved between page requests in PHP by using "Session Variables". Session variables preserve their values inbetween page loads, allowing you to store information about the user's current "session". The most typical usage of session variables is to keep track of whether a user is logged in or not, and if they are logged in, under which username they logged in with. When the user leaves the website, closes their browser, or just waits for a long enough period of time, the session is discarded. Sessions are temporary variables that persist between page loads.

Permanent information can be stored using PHP code either in a) A database on the server, or b) In files on the server. Using a database is preferred. Far more reliable. Your PHP page should accept data from the user via a form submission, and then store that data into, say, a MySql database. When the user views a page, the PHP script should query the database, grab the appropriate information, and write it out as part of the page it is returning to the user.

What exactly it is you store and retrieve is up to you. If you want to store the user's information raw, that's fine. If you want to pass it through an array and store the data in the array instead, that's up to you as well. But in the end, the user submits data via a form, it is massaged by PHP in some way, and then it is stored on the server in a database or in a file.

PHP has a complete set of filesystem functions for opening, editing, creating and deleting files on the server, but your server must give PHP permission to do these actions. Most servers don't give that kind of permission out of general security concerns.


PHP also has a fairly complete set of database functions for connecting to, querying, updating and inserting into mysql and microsoft sql server databases


microsoft sql server:

To use a database you need to have a database engine running on your server, and you need to have created credentials for logging onto that database, you need tables, columns, etc. already created in the database.

A large number of tutorials both on using MySql, and on manipulating files on the server are available here:


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Another example available on W3Schools starts right from the beginning concerning MySql, and how to use it in PHP, and works forward slowly, first showing how to create tables on the MySql server, and then how to store and retrieve data from those tables using PHP:


A few things to note about using MySql and PHP, so that you know what is your responsibility, and what is your web host's responsibility:

      - MySQL needs to already be installed and configured on your web host's server. You might have to pay more money for the priviledge of having database access.

      - Your web host must provide you with the hostname, username, password and database name. Those bare essentials are required to connect to MySQL from PHP and should be provided to you by your web host. If the hostname is not provided to you, it is most likely "localhost", or "". You may have to go through a configuration or setup via the web host's control panel to set that up.

      - You web host should also provide you with a tool to administer the MySQL databases that you have access to. Usually they provide you with a link to PHPMyAdmin - a free web based mysql administration tool that you typically use to administer the database. This way, you have a reliable way of looking at what's in your mysql database.


Thanks...I'll study this for a bit.  The theory part is straightforward (database/mysql better etc) but I'm more concerned about learning php than mysql.  I think I need to assign session variables to the array but I'm not clear about how  that works so I'll poke around php.net, thanks for the link...with nested arrays it's difficult to sort out which part is a key and which part is a value so hopefully the echo/print statements will help me do that.


I'm still trying to pass session variables, unsuccessfully.  I've read http://us3.php.net/manual/en/function.session-start.php but don't understand how to apply the example to my situation. I copy my code below.

# page 1.php
include mySchedule.php; //this is the array with the weekdays, hours and tasks I posted earlier.
...omit table with select statements to view the schedule for the whole week, one day or one hour of one day...
<input type="hidden" name="sid" value="<?php echo $sid; ?>" />
<input type="submit" value="change">
# The html form variables print:
$days = $_POST['form_days'];  
$hours = $_POST['form_hours']; 
# None of these session variables are carried over:
$_SESSION['form_days']= $session_days;  
$_SESSION['form_hours']= $session_hours; 
...html form follows with a text box for user schedule change input...
I tried  using only session variable definitions, then reversing them as follows, but all fail to carry over:
$session_days = $_SESSION['form_days'];
$session_hours = $_SESSION['form_hours'];
$session_id = $_SESSION['sessionID'];
I'm using echo/print statements to see if the variables are being passed from page 1 to page 2.  I'm not at liberty to change the php.ini file on the server (I know that the server processes session variables in Perl and don't believe that the problem lies with php.ini). I have tried the above with firefox and with the (default) konqueror web browser on my kubuntu client so I don't think the problem is with the client either, but I'm open to suggestions! 

Open in new window


The post above was a bit muddled with comments in the code box, so I copy the comments below, sorry about that.  Also, when I changed $sid to $session_id, I made sure to use the same variable names on both page 1 and page 2.
I'm using echo/print statements to see if the variables are being passed from page 1 to page 2.  I'm not at liberty to change the php.ini file on the server (I know that the server processes session variables in Perl and don't believe that the problem lies with php.ini). I have tried the above with firefox and with the (default) konqueror web browser on my kubuntu client so I don't think the problem is with the client either, but I'm open to suggestions!


It was a "started to help" question that wasn't followed up...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.