Hi!
I'm having problems in getting the value of the selected item and showing the items in the second dropdown. When I select a category on the first menu, the choices on the second menu appears but the selected choice doesn't show in the dropdown. And i don't get the value of the selected item. How do i make the dynamic drop down work properly and get the values of the selected items in both menus so that i can use them in the next page. really need help here! thanks!

<?php
session_start();
	if(!session_is_registered(myusername)){
		header("Location:main_login.php");
 	}
?>
<html>
<head>

<title>Employee</title>

<style type="text/css">
<!--
.tableHead {
	font-family: "Myriad Pro Light", "Myriad Pro";
	font-size: 17px;
	font-style: normal;
	font-weight: bold;
	color: #FFF;
}
.tableHead1 {
	font-family: "Myriad Pro Light", "Myriad Pro";
	font-size: 15px;
	font-style: normal;
	font-weight: bold;
	color: #346198;
}
.tableHead2 {
	font-family: "Myriad Pro Light", "Myriad Pro";
	font-size: 14px;
	font-style: normal;
	font-weight: bold;
	color: #FFF;
	
}
.tableContent {
	font-family:Euphemia;
	font-size: 13px;
	font-style: normal;
	color: #000;
}
-->
</style>


<script type="text/javascript">

function confSubmit(form) {
	if (confirm("Are you sure you want to send this report?")) {
		window.location("userMy_Asset.php")
	} else {
		alert("You decided to not submit the report!");
		window.location("mainUser.php")
	}
}

function reload(form){
	var val=form.cat.options[form.cat.options.selectedIndex].value; 
	self.location='dd.php?cat=' + val ;
}
</script>
</script>

</head>



<body>
<table width="914" height="154" border="0">
  <tr>
    <td width="840" height="23" colspan="5" bgcolor="#346198" class="tableHead">Report Asset</td>
  </tr>
  <tr>
    <td height="12" colspan="5"></td>
  </tr>
  <form action="userReport_Confirm.php" method="post"><tr>
    <td height="83" colspan="5"><table width="501" height="115" order="0" style="text-align: center; font-weight: normal; border-collapse:collapse; border:1px solid #346198" cellpadding="3" cellspacing="3">
      <tr>
        <td height="21" colspan="3" class="heading4" bgcolor="#346198"><div align="left" class="tableHead">Create New Ticket</div></td>
      </tr>
      <tr>
        <td width="84" height="27" class="heading4"><div align="left">Category:</div></td>
        <td width="148"><div align="left">
        <select name="category" onchange="this.form.submit()">
          <option>Select a category</option>
          <option value="Hardware">Hardware</option>
          <option value="Software">Software</option>
        </select>
        </div>
          </td>
        <td width="206"><div align="left">
          <select name="subcategory" onchange="this.form.submit()">
          <option>Select a subcategory</option>
         	<?php
				include "..\configdb.php";
				include "..\opendb.php";
				
				$category = $_POST['category'];
				session_register("category");
				$_SESSION['category']=$category; 
				
				if(!strcmp($_POST['category'],"Hardware")){

					$id = $_SESSION['id'];
					
					$result = mysql_query("SELECT _subcategory FROM inventory_hardware WHERE _assignedTo='$id'");
					
					if(!$result) {
						echo 'Could not run query: ' . mysql_error();
						exit;
					}
					
					while($row=mysql_fetch_array($result))
					{
						$i;
						$option = $row['_subcategory'];
						
						echo "<option value='$option'>$option</option>";
		
						$i++;
					}	// end while loop
		
					
					//get asset control number
					$result = mysql_query("SELECT _controlNumber FROM inventory_hardware WHERE _assignedTo='$id' and _subcategory='$_POST[subcategory]'");
					while($row=mysql_fetch_array($result))
					{
						$controlNum = $row['_controlNumber'];
						
					}
					$_SESSION['controlNum']= $controlNum;
					$subcategory = $_POST['subcategory'];
					session_register("subcategory");
					$_SESSION['subcategory']=$subcategory; 
				}
				if(!strcmp($_POST['category'],"Software")){

					$id = $_SESSION['id'];
					
					$result = mysql_query("SELECT _subcategory FROM inventory_software WHERE _assignedTo='$id'");
					
					if(!$result) {
						echo 'Could not run query: ' . mysql_error();
						exit;
					}
					
					while($row=mysql_fetch_array($result))
					{
						$i;
						$option = $row['_subcategory'];
						
						echo "<option value='$option'>$option</option>";
		
						$i++;
					}	// end while loop
		
					
					//get asset control number
					$result = mysql_query("SELECT _controlNumber FROM inventory_software WHERE _assignedTo='$id' and _subcategory='$_POST[subcategory]'");
					while($row=mysql_fetch_array($result))
					{
						$controlNum = $row['_controlNumber'];
						
					}
					$_SESSION['controlNum']= $controlNum;
					$subcategory = $_POST['subcategory'];
					session_register("subcategory");
					$_SESSION['subcategory']=$subcategory; 
				}

			?>
        </select>
        </div></td>
        <?php
		echo "Category is: ".$_SESSION['cat'];
		echo "Sub-Category is: ".$_SESSION['subcat'];
        echo "Control Number is: ".$_SESSION['controlNum'];
		?>
        </tr>
      <tr>
        <td height="11" class="heading4"><div align="left">Problem:</div></td>
        <td colspan="2" rowspan="2"><div align="left"><textarea name="problem" cols="45" rows="3"></textarea></div></td>
      </tr>
      <tr>
        <td height="11" class="heading4">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  
    <tr>
      <td width="64" height="26" class="heading4">
        <input type="submit" value="Submit" />
      </form>
</table>
</body>
</html>

Recommended Answers

i think first of all u have to submit form ones instead of twice.
"<select name="category" onChange="this.form.submit()">" this is because when form post twice it lost the vallues. if you want to post twice then make querystring for category dropdown so that the value doesn't change.


second after …

Jump to Post

You have onChange set up to submit the form when Category changes, what is it that you were expecting it do?

Jump to Post

All 7 Replies

i think first of all u have to submit form ones instead of twice.
"<select name="category" onChange="this.form.submit()">" this is because when form post twice it lost the vallues. if you want to post twice then make querystring for category dropdown so that the value doesn't change.


second after selection of first (category) dropdown it make query like

$result = mysql_query("SELECT _subcategory FROM inventory_hardware WHERE category like"$category"'");

OR

$result = mysql_query("SELECT _subcategory FROM inventory_hardware WHERE category = "$category"'");


this wil get all the subcategories from table related to main categories.

thanks! but what happens now is when i choose a category, it automatically redirects to the next page. how do i fix that?

You have onChange set up to submit the form when Category changes, what is it that you were expecting it do?

Hi wish02

Its because you pas action "userReport_Confirm.php" in form in your code when any category value change at that time due to "this.form.submit();" your form will be submit and thats why it redirect to action page.

if you want to avoid it then you have to stop page refreshed or have to avoid this.form.submit(); method.

Other way to achieve your work is via AJAX.by ajax function you can change subcategory based on category selection.

I think this would be more appealing to users if you populate the subcategories just as done in the link

http://www.raveportal.com/questionpage.php

Also you can use the framework like jquery to do this stuff...its very easy...

You will get loads of tutorials on http://www.tutorialized.com

If you want, mail me your db dump...and I will make you the script in AJAX if needed.

Hi due to weekend unable tp reply soon.

well there are simple ways to do the redirect stuff, first of all make action of form empty like action=""

now there are two ways to redirect form java script when button (submit) clicks

after validation form it will do

window.location = "mypage.php";

and there is also other easy way to redirect by combining php and java

that is make name of button like

<input name="action" type="submit" class="input-Button" id="actaction" value="Submit" />


on click get action in php like

&action = &_post;

if(isset($action) && $action == "Submit"){
?>
<script type="text/javascript">
window.location = "mypage.php";
</script>
<?
}

Hope this will work for u. if it works then plz dont forget to mark this thread as solved. thanx.

thanks everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.