Link to home
Start Free TrialLog in
Avatar of sara_bellum
sara_bellumFlag for United States of America

asked on

how to save variables passed from an html form in php

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 :(
<?php
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", ...
etc
# 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></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="" />
</td></tr>
<tr><td align="center" colspan=2>
<input type="submit" value="Change"/>
</form></td></tr></table>
<p>Return to <a href="daily_schedule.html">form</a></p>
 
...
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Frosty555
Frosty555
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:

http://www.w3schools.com/PHP/php_mysql_intro.asp

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 "127.0.0.1". 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.
Avatar of sara_bellum

ASKER

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
 
<?php
session_start();
 
$_SESSION['form_days']=$session_days;
$_SESSION['form_hours']=$session_hours;
$_SESSION['sessionID']=$sid;
 
include mySchedule.php; //this is the array with the weekdays, hours and tasks I posted earlier.
 
?>
<html>
...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">
 
#page2.php
<?php
session_start();
 
# 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; 
$_SESSION['sessionID']=$sid;
?>
...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...