- 2,492
Hi all,
I am attempting to develop a registration system for a login system (which I have yet to build) that uses PHP sessions to pass data from a user_registration.php file to a register.php file. However, I am having difficulty setting the select.selectedIndex field of a select dropdown menu using sessions, nonnumeric data, hidden fields, and javascript. Can someone look over my code and see if anything is wrong with my code? I apologize about so much code being included but I wouldn't be able to really explain it otherwise.
The state_sel and grade_sel variables are declared in a session_init.php file (not all code is shown for the sake of space):
An data array is set up for specific values that will be used in session_func.php:
user_registration.php (not all code is shown for the sake of space). This file contains my registration form with the hidden fields and javascript that is supposed to set the values for my hidden fields.
My javascript validation method that is "supposed" to pass the values of my hidden fields but seems to pass the default values:
The hidden fields in my form located in user_registration.php:
If the user is redirected from register.php (see below) back to the user_registration.php page, it is supposed to display previously entered values including those in the drop down box. However, I am sending non-numeric data so I am unsure as to why select.selectedIndex is not working here:
register.php determines if values are entered and redirects back to user_registration.php if they aren't. Also, it reads the posted variables from $_POST array into the $_SESSION array:
I am attempting to develop a registration system for a login system (which I have yet to build) that uses PHP sessions to pass data from a user_registration.php file to a register.php file. However, I am having difficulty setting the select.selectedIndex field of a select dropdown menu using sessions, nonnumeric data, hidden fields, and javascript. Can someone look over my code and see if anything is wrong with my code? I apologize about so much code being included but I wouldn't be able to really explain it otherwise.
The state_sel and grade_sel variables are declared in a session_init.php file (not all code is shown for the sake of space):
PHP:
<?php
//session_init.php
$_SESSION['GRADE_SEL'] = "";
$_SESSION['STATE_SEL'] = "";
$_SESSION['SESSION'] = true;
?>
PHP:
<?php
//session_func.php
//setup a dummy array that will contain values that we want to work with...
$arVals = array('grade_sel'=>"", 'state_sel'=>"", 'fname'=>"", 'lname'=>"", 'email'=>"", 'school'=>"", 'grade'=>"", 'address'=>"",
'city'=>"", 'county'=>"", 'state'=>"", 'city'=>"", 'zip'=>"", 'phone'=>"");
?>
PHP:
<?php
//user_registration.php
session_start();
if (!isset($_SESSION['SESSION'])) require("session_init.php"); //If session does not exist then it is started here.
$arVals = array();
require_once("session_func.php");
//make sure the session values are initialized...
reset($arVals);
while(list ($key, $val) = each($arVals)) {
if (!isset($_SESSION[$key])) $_SESSION[$key] = "";
if ($_SESSION[$key]== "NULL") $_SESSION[$key] = "";
if ($debug) echo $key . " : " . $arVals[$key] . "<br />";
}
if ($_SESSION['grade_sel'] == "") $_SESSION['grade_sel'] = 0;
if ($_SESSION['state_sel'] == "") $_SESSION['state_sel'] = 0;
?>
PHP:
//user_registration.php
<script language="javascript" type="text/javascript">
function SubmitForm()
{
var form = document.forms[0];
var bRequired = true;
//Client-side field validation to determine if required fields are blank.
if ((form.firstname.value.length < 1) ||
(form.lastname.value.length < 1) ||
(form.school.value.length < 1) ||
(form.county.value.length < 1) ||
(form.email.value.length < 1) {
alert("Please fill out all the required fields.");
bRequired = false;
}
if (!bRequired) return false;
form.grade_sel.value = form.grade.selectedIndex; //gets the selected value of the grade selection dropdown
form.state_sel.value = form.state.selectedIndex; //gets the selected value of the state selection dropdown
form.submit();
}
</script>
PHP:
//user_registration.php
<input name="grade_sel" type="hidden" value="<?php echo $_SESSION['grade_sel']; ?>" /> <!-- Used to update selection field -->
<input name="state_sel" type="hidden" value="<?php echo $_SESSION['state_sel']; ?>" />
PHP:
//user_registration.php
<script language="javascript" type="text/javascript">
//Set the selection box values...
var form = document.forms[0];
form.grade.selectedIndex = parseInt("<?php echo $_SESSION['grade_sel']; ?>");
form.state.selectedIndex = parseInt("<?php echo $_SESSION['state_sel']; ?>");
</script>
register.php determines if values are entered and redirects back to user_registration.php if they aren't. Also, it reads the posted variables from $_POST array into the $_SESSION array:
PHP:
//register.php
session_start();
if (!isset($_SESSION['SESSION'])) require("session_init.php"); //If session does not exist then it is started here.
$arVals = array();
require_once("session_func.php");
reset($_POST);
//Gets $_POST values and adds them to session variables.
while (list($key, $val) = each ($_POST)) {
if ($val == "") $val = NULL;
$arVals[$key] = (get_magic_quotes_gpc()) ? $val : addslashes($val); //Removes single quotes and double quotes to protect DB.
if ($val == "NULL") {
$_SESSION[$key] = NULL;
}
else
{
$_SESSION[$key] = $val;
//if ($debug) echo $key . " : " . $arVals[$key] . "<br />";
if ($debug) echo $key . " : " . $_SESSION[$key] . "<br />";
}
}