Good day all!
I have been working on a project that requires me to to the following:
Select a user from a list, on submit go to next page.
The next page contains the session variable $id of the selected user.
On this page, I am assigning menu permissions. I have a repeating row from a recordset and I am getting the values for insertion or update from the form correctly on insert/update, but I can't seem to get the select box "menuassignment[]" to show the users input on error checking. I think it has something to do with how I am going about getting the array values, but have not been able to get it to work.
Here is the code for the page:
/////////////////////////////
$expected = array('menuassignment');
$missing = array();
// assume that there is nothing suspect
$suspect = false;
/////////////////////////////////////
$AddData="no";
$warnstart="no";
$MM_flag="MM_insert";
if((isset($_POST["Submit"])) && ($_POST["MM_insert"] == "access1")) {
$AddData="yes";
$warnstart="no";
if (!$_POST['restrict'])
{
$AddData = "no";
?>
<?php
if ($warnstart=="no") { ?>
<div class="warningstart">Please complete the missing item(s) indicated. </div>
<?php
}
$warnstart="yes";
if (!$_POST['restrict']) { ?> <div class="warning"><li>Please set the Restrict Record Edit Options</li></div>
<?php }
}
for($i=0;$i<count($_POST['menuname']);$i++)
{
$assigned=$_POST['menuassignment'][$i];
$MName=$_POST['menuname'][$i];
$id=$_SESSION['mambo_user_id'];
///echo out error message
if ($assigned =="")
{
$AddData = "no";
if ($warnstart=="no")
{ ?>
<div class="warningstart">Please complete the missing item(s) indicated. </div>
<?php
}
$warnstart="yes";?>
<div class="warning"><li>Please set the Menu Assignment Options for <? echo $MName; ?> Menu</li></div>
<?php //}
}
}
if ($warnstart=="yes")
{ ?>
<div class="warningend"></div>
<?php
}
}
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
if((isset($_POST["Submit"])) && ($_POST["MM_insert"] == "access1") && $AddData=="yes" && isset($_POST['menuname']))
{
$updateApps= sprintf("UPDATE `skills_access` SET `restrict`=%s WHERE `mambo_user_id`=%s AND `skills_access_id`=%s ",
GetSQLValueString($restrict, "text"),
GetSQLValueString($mambo_user_id, "text"),
GetSQLValueString($AccessID, "text"));
mysql_select_db($database_training, $training);
$result2 = mysql_query($updateApps, $training) or die(mysql_error());
if((isset($_POST['menuname']))|| (isset($_POST['menuassignment']))|| (isset($_POST['id']))|| (isset($_POST['username'])))
{
for($i=0;$i<count($_POST['menuname']);$i++)
{
$assigned=$_POST['menuassignment'][$i];
$MName=$_POST['menuname'][$i];
//$id=$_POST['mambo_user_id'];
$menu=$_POST['menu'][$i];
$username = $row_rsUser['username'];
//insert statement goes here
$insertApps = "INSERT INTO assignedmenu(`username`, `mambo_user_id`, `Access`, `menuname`, `MenuID`) VALUES('$username', '$id', '$assigned','$MName', $menu)";
$result1 = mysql_query($insertApps) or die(mysql_error());
}
}
///////////////////////
if ($suspect) {
//$mailSent = false;
unset($missing);
}
else {
// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
}
if (!$suspect && empty($missing)) {
// set default values for variables that might not exist
$menuassignment = isset($menuassignment) ? $menuassignment : array('None selected');
}
///////////////////////////
// here put the insertgoto page
//$insertGoTo = "access2.php";
//header(sprintf("Location: %s", $insertGoTo));
}
?>
Here is the form:
<? $_SESSION['AccessID']; ?>
<form id="access1" name="access1" method="POST" >
<table width="550" border="1" bordercolor="#000066">
<tr>
<td colspan="2" scope="row"><div align="center"><strong>Training Database Menu Security & Restrict Record Edit </strong></div></td>
</tr>
<tr>
<td colspan="2" scope="row"><div align="left"><b>
<?php echo $row_rsUser['username']; ?>
<input name="username" type="hidden" value="<? echo $row_rsUser['username'] ; ?>" />
<input name="id[]" type="hidden" value="<? echo $_SESSION['mambo_user_id']; ?>">
<input name="skill_access_id" type="hidden" value="<? echo $_SESSION['AccessID']; ?>" />
</div></td>
</tr>
<tr>
<td width="218" valign="middle" scope="row"><? echo $var; ?></td>
<td width="316" valign="middle" scope="row">Restrict Record Edit?
<?php
// initialize or capture variable
$XX = !isset($_POST['restrict'])? NULL : $_POST['restrict'];
?>
<select name="restrict">
<option value="<?php echo $XX;?>" SELECTED><?php echo $XX;?></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
</tr>
<tr>
<td height="46" colspan="2" valign="middle" scope="row"><div align="center"><span class="style1"><em>Please indicate which menus the user will have access to</em>. </span></div></td>
</tr>
</table>
<table width="550" border="1" bordercolor="#00066">
<?php do { ?>
<tr>
<td width="227" valign="middle" scope="row"><B>
<input name="menu[]" type="hidden" id="menu[]" value="<?php echo $row_rsMenu['MenuID']; ?>" />
<?php echo $row_rsMenu['menuname']; ?>
<input name="menuname[]" type="hidden" value="<? echo $row_rsMenu['menuname'] ; ?>" />
</B></td>
<td>
///////////////////////////////////////////////////////////////////////////////////////////////////////
as you can see by the commented out code, I have tried to get this to work on several ways. All I want to do here is :
if the user hits submit and has not filled out the form properly, I want the users initial selected values retained for output to the screen.
///////////////////////////////////////////////////////////////////////////////////////////////////
<? //for($i=2;$i<count($_POST['menu']);$i++)
//{
//$menuassignment= array();
//foreach ($menuassignment as $item)
//{
//echo "the values of menuassignment are '".$item."'<br/>";
//}
//$menuassignment=$_POST['menuassignment'][$i]; ?>
<select name="menuassignment[]">
<option value=""
<?php
$OK = isset($_POST['menuassignment']) ? true : false;
if ($OK && isset($missing) && in_array('', $_POST['menuassignment'])) { ?>
selected="selected"
<?php } ?>></option>
<option value="Yes"<?php if ($OK && isset($missing) && in_array('Yes', $_POST['menuassignment'])) { ?>
selected="selected"<?php } ?>>Yes</option>
<option value="No"<?php if ($OK && isset($missing) && in_array('No', $_POST['menuassignment'])) { ?>selected="selected"<?php } ?>>No</option>
</select>
<? //} ?>
</tr>
<?php } while ($row_rsMenu = mysql_fetch_assoc($rsMenu)); ?>
<tr>
<td colspan="2" valign="middle" scope="row"><div align="center">
<input name="Submit" type="Submit" id="Submit" value="Submit" />
<input type="button" name="Cancel" value="Return to Access Menu" onClick="self.location='index.php'" />
</div></td>
</tr>
<? //} ?>
</table>
<input type="hidden" name="MM_insert" value="access1">
</form>
<pre>
<?php
if ($_POST) { print_r($_POST); }
?>
</pre>
</body>
</html>
<?php
mysql_free_result($rsApps);
mysql_free_result($rsUser);
mysql_free_result($rsAppName);
mysql_free_result($rsMenu);
//mysql_free_result($rsYN);
mysql_free_result($rsRestrict);
mysql_free_result($rsAccess);
//ob_flush();
?>
If any one can help get me going in the right direction here, I would greatly appreciate it.