PHP reload

Hi,

I have a problem in submitting a form in my php…after submitting my page…it must display my value in tbluserlevel and tbldept in select but it does not…What i’am doing wrong…thx…

Here is my complete script…


<?php
session_start();
include_once("conn.php");
if(isset( $_SESSION['username'])){
$userlevel=mssql_query("Select * from tblUserLevel");
$deptname=mssql_query("SELECT * FROM tblDept"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">
<head>
	<title>Create User</title>
	<link href="css/mystyle.css" rel="stylesheet" type="text/css"/>
	<script src="jquery\\jquery.min.js"></script>
	<script type="text/javascript" src="jquery\\jquery.validate.js"></script> 
  <script>
  $(document).ready(function(){
  
	$.validator.addMethod("noSpecialChars", function(value, element) {
		  return this.optional(element) || /^[a-z0-9\\_]+$/i.test(value);
	  }, "<br/><font size='3' color='#FF0000'>Username must contain only letters, numbers, or underscore.</font>");
	 
    $("#myadduser").validate({
	  rules: {
		pass: "required",
		rpass: {
		  equalTo: "#pass"
		},
		uname: "required noSpecialChars",
		fname:"required",
		lname:"required",
		email:"required email",
		dept:"required",
		userlvl:"required "
	  }
	  
	});
  });
  </script>
</head>
<body onLoad="document.myadduser.uname.focus()";>

<fieldset class='add' >
<legend>Add User Information </legend>
<br/>
<?php
$userlevel=mssql_query("Select * from tblUserLevel");
$deptname=mssql_query("SELECT * FROM tblDept");

function assign($Assign,$con){
	mssql_query("Insert into MISPIC (MISPIC) values ('$Assign')");
	mssql_close($con);
}	
function Insert($uname,$pass,$fname,$lname,$email,$dept,$userlvl,$CreateBy,$con){
 	mssql_query("sp_insertuser @uname='$uname',@lname='$lname',@fname='$fname',@email='$email',@pass='$pass',@dept='$dept',@userlvl='$userlvl',@Cby='$CreateBy'"); 
	mssql_close($con);
}

if($_POST['uname']!=""){		
	if(mssql_num_rows(mssql_query("Select UserName from tblUser where UserName='$_POST[uname]'"))>0){
		echo "&nbsp; &nbsp;<font color='#FF0000' size='3'>Username already exist.</font><br/>"; }
		
		
	else{
		
		Insert(trim($_POST['uname']),md5(trim($_POST['pass'])),trim($_POST['fname']),trim($_POST['lname']),trim($_POST['email']),trim($_POST['dept']),trim($_POST['userlvl']),$_SESSION['username'],$con);
		echo "&nbsp; <font size='3' color='#ff0000'>User <strong>".$_POST['uname']."</strong> has been save.</font>";
		
		if($_POST['userlvl']=="Admin" OR $_POST['userlvl']=="Support"){
		assign($_POST['userlvl'],$con);	
		}
		
	unset($_POST['uname']);
	unset($_POST['fname']);
	unset($_POST['lname']);
	unset($_POST['email']);	
	
	}
}

?>
	<table>
		<form name='myadduser' id='myadduser' action='<?php echo $_SERVER['PHP_SELF']; ?>' method='Post' onSubmit="javascript: var x=window.confirm('Do you wan\\'t to save this User?');if (!x) return(false);">
			
			<tr>
				<td> User Name : </td>
				<td align='left'><input id='uname'  name='uname' type='text'  value='<?php if(isset($_POST['uname'])) echo trim($_POST['uname']); ?>' tabindex='1'></td>				
			</tr>
			<tr>
				<td> First name : </td>
				<td align='left'><input id='fname' type='text' value='<?php if(isset($_POST['fname'])) echo trim($_POST['fname']); ?>' name='fname' tabindex='2'></td>
				
			</tr>
			<tr>
				<td> Last name : </td>
				<td align='left'><input id='lname' type='text'  value='<?php if(isset($_POST['lname'])) echo trim($_POST['lname']); ?>' name='lname' tabindex='3'></td>
			</tr>
			<tr>
				<td> Password : </td>
				<td align='left'><input id="pass"  type="password" name="pass" tabindex="4" ></td>
				
			</tr>
			<tr>
				<td> Re-Type Password : </td>
				<td align='left'><input id='rpass'  type='password' name='rpass' tabindex='5'></td>
				
			</tr>
			<tr>
				<td> Email Address : </td>
				<td align='left'><input id='email' size='35'  type='text'value='<?php if(isset($_POST['email'])) echo trim($_POST['email']); ?>' name='email' tabindex='6'></td>
			</tr>	
			<tr>
				<td> Department: </td>
					<td align='left'>
					<?php 
						echo "<select class='dept' name='dept' tabindex='9'>";
						echo "<option></option>";
						while($dept=mssql_fetch_array($deptname)){
							echo "<option  value='$dept[DeptName]'>$dept[DeptName]</option>";
						}echo '</select>';
					?>
					</td>
			</tr>
			<tr>
				<td> User Level : </td>
				<td align='left'>
				<?php 
					echo "<select class='userlvl' name='userlvl'tabindex='10'>";
					echo "<option></option>";
					while($ulvl=mssql_fetch_array($userlevel)){
						echo "<option size='5' value='$ulvl[UserLevel]'>$ulvl[UserLevel]</option>";
					}echo '</select>';
				?>
				</td>
			</tr>
			<tr>
				<td><input type='submit' name='save' value='Save' ></td>
				<td><input type='reset' name='reset' value='Reset'></td>
			</tr>	
		</form>
	</table>	
</fieldset>	
</body>
</html>
<?php
}else{ //not logged in
    header('location: login.php');
}
?>


I think this is your problem


echo "<option  value='$dept[DeptName]'>$dept[DeptName]</option>";

SB


echo "<option value='" . $dept['DeptName'] . "'>" . $dept['DeptName'] . "</option>";

PHP will not expand array elements inside a string as you tried, so concatenate them correctly. In fact this might be better, use single quotes and save the double quotes so that your html will me more likely to validate (depending on your DTD declaration).


echo '<option value="' . $dept['DeptName'] . '">' . $dept['DeptName'] . '</option>';

Also, you should quote the array keys ‘DeptName’, notice carefully that if deptname contained spaces “After Sales” you must quote them in your html otherwise they will not arrive as you expect because they will be truncated at the first space.

<option value=After Sales>After Sales</option>

will result in After%20Sales or just plain “After” (IIRC)

Thanks…that’s a help…