Multiple Select Elements

I have been messing with this for a few days now and I have come to a dead end. I don’t consider my self good at PHP coding. I have 4 “Select” elements and the names for them are sub[1] sub[2] sub[3] and sub[4]. I have a jQuery that displays the different one depending on the previous select element. In my PHP code I am trying to make it find the “sub” that the user has chosen from.

HTML:

<div class="input-group item">
				  <span class="input-group-addon">Type</span>
				  	<select id="type" name="type" class="form-control" onclick="typefunction(); itemstats(); Armor();">
				          <option value="Any Type">Any Type</option>
				          <option value="1-Hand">1-Hand</option>
				          <option value="2-Hand">2-Hand</option>
				          <option value="Armor">Armor</option>
				          <option value="Off-Hand">Off-Hand</option>
				    </select>
				</div>
				<div class="input-group item">
				  <span class="1-Hand input-group-addon" style="display: none;">Sub-Type</span>
					<select class="1-Hand form-control" name="sub[1]" style="display: none;">
						  <option value="All 1-Hand Item Types">All 1-Hand Item Types</option>
						  <option>Axe</option>
						  <option>Ceremonial Knife</option>
						  <option>Hand Crossbow</option>
						  <option>Dagger</option>
					      <option>Fist Weapon</option>
					      <option>Mace</option>
					      <option>Mighty Weapon</option>
					      <option>Spear</option>
					      <option>Sword</option>
					      <option>Wand</option>
				  	</select>
				  	</div>
				
				 <div class="input-group">
				  <span class="2-Hand input-group-addon" style="display: none; ">Sub-Type</span>
					<select class="2-Hand form-control" name="sub[2]" style="display: none;">
						  <option>All 2-Hand Item Types</option>
						  <option>Two-Handed Axe</option>
						  <option>Bow</option>
						  <option>Diabo</option>
						  <option>Crossbow</option>
						  <option>Two-Handed Mace</option>
						  <option>Two-Handed Mighty Weapon</option>
						  <option>Polearm</option>
						  <option>Staff</option>
						  <option>Two-Handed Sword</option>
				  	</select>
				  	</div>

				
				  <div class="input-group">
				  <span class="Armor input-group-addon" style="display: none;">Sub-Type</span>
					<select class="Armor form-control" name="sub[3]" style="display:none;">
				  		  <option>All Armor Item Types</option>
						  <option>Amulet</option>
						  <option>Belt</option>
						  <option>Boots</option>
						  <option>Bracers</option>
						  <option>Chest Armor</option>
						  <option>Cloak</option>
						  <option>Gloves</option>
						  <option>Helm</option>
						  <option>Pants</option>
						  <option>Mighty Belt</option>
						  <option>Ring</option>
						  <option>Shoulders</option>
						  <option>Spirit Stone</option>
						  <option>Voodoo Mask</option>
						  <option>Wizard Hat</option>
				  	</select>
				  	</div>
			
			    <div class="input-group">
				  <span class="off input-group-addon" style="display: none;">Sub-Type</span>
					<select class="off form-control" name="sub[4]" style="display:none;">
						  <option>All Off-Hand Item Types</option>
						  <option>Mojo</option>
						  <option>Source</option>
						  <option>Quiver</option>
						  <option>Shield</option>
				  	</select>
				  	</div>

PHP:

$subType = $_POST['sub'][1];
$TwoHand = $_POST['sub'][2];
$Armor = $_POST['sub'][3];
if ($subType == 'All 1-Hand Item Types') {
    $Armor = $subType;
}

JQuery:

function typefunction() {
    var itemTypes = jQuery('#type');
    var select = this.value;
    itemTypes.change(function() {
        if ($(this).val() == '1-Hand') {
            $('.1-Hand').show();
            $('.2-Hand').hide();
            $('.off').hide();
            $('.Armor').hide();
        }
        else $('.1-Hand').hide();
        $('.off').hide();
        if ($(this).val() == '2-Hand') {
            $('.2-Hand').show();
            $('.1-Hand').hide();
            $('.off').hide();
            $('.Armor').hide();
        }
        else $('.2-Hand').hide();
        $('.off').hide();
        if ($(this).val() == 'Armor') {
            $('.Armor').show();
            $('.2-Hand').hide();
            $('.off').hide();
            $('.1-Hand').hide();
        }
        else $('.Armor').hide();
        $('.off').hide();
        if ($(this).val() == 'Off-Hand') {
            $('.off').show();
            $('.2-Hand').hide();
            $('.1-Hand').hide();
            $('.Armor').hide();
        }
        else $('.off').hide();
    });
}
  1. What are you expecting?
  2. What actually happens?

I want it to see if sub[1] == “All 1-Hand Item Types” if it does equal that then it looks to see what sub[2] is and if that == “All 2-Hand Item Types” then it moves to the third one and so on.

It does absolutely nothing, it doesn’t get to the “INSERT INTO”

All of your <option> need value=“something” for your $_POST[‘sub’][1] (and [2] and [3] and [4]) to have a value. That might be the source of the problem. Hard to tell since there isn’t much PHP code posted here. Even if that isn’t what’s causing this issue, you’re going to need those values in there so I’d say start with that and see what happens.

This is the entire code:

&lt;?php

class Account extends MainController{
	function __construct(){
			parent::__construct();
			if($_SESSION['user'] == ""){
				header("location:Logout");
				exit(0);
			}else{
				
				$sql = "SELECT * FROM users WHERE username = :username ";
				$arr = array(":username" =&gt; $_SESSION['user']);
				$ctr = $this-&gt;database-&gt;DBCtr($sql,$arr);
				
				if($ctr &gt; 0){
					$usr = $this-&gt;database-&gt;DBQry($sql,$arr);
					$this-&gt;view-&gt;usr = $usr[0]['username'];
					$this-&gt;view-&gt;pss = $usr[0]['password'];
					$this-&gt;view-&gt;name = $usr[0]['user_id'];
				}else{
					header("location:Index");
					exit(0);
				}
				
			}
	}
	
	function ChangePassword(){
		$this-&gt;view-&gt;url	=	$this-&gt;config-&gt;url;
		$this-&gt;view-&gt;ID 	  = get_class($this);
		$this-&gt;view-&gt;Title = "Royal JD &gt; Account &gt; Change Password";
		$this-&gt;view-&gt;render('Account/ChangePassword');
	}
	
	function ChangePasswordSubmit(){
		
		$this-&gt;view-&gt;url	=	$this-&gt;config-&gt;url;
		$this-&gt;view-&gt;ID 	  = get_class($this);
		$this-&gt;view-&gt;Title = "Royal JD &gt; Account &gt; Change Password";
		
		$new = isset($_POST['new']) ? $_POST['new'] : '';
		$old = isset($_POST['old']) ? $_POST['old'] : '';
		$con = isset($_POST['con']) ? $_POST['con'] : '';
		$this-&gt;view-&gt;usr = mysql_real_escape_string(StrToLower(Trim($this-&gt;view-&gt;usr)));
		$new = mysql_real_escape_string(StrToLower(Trim($new)));

		$this-&gt;view-&gt;salt = $usr[0]['username'];
		$this-&gt;view-&gt;salt = $this-&gt;view-&gt;name.$new;
		$this-&gt;view-&gt;salt = md5($this-&gt;view-&gt;salt);
		$this-&gt;view-&gt;salt = "0x".$this-&gt;view-&gt;salt; //Salts the password in md5.
		$this-&gt;view-&gt;msg = "";
		$this-&gt;view-&gt;err = false;
		
		if($old != $this-&gt;view-&gt;pss){
			$this-&gt;view-&gt;msg = "Old Password is Incorrect";
		}elseif($new == ""){
			$this-&gt;view-&gt;msg = "New Password is Empty";
		}elseif($con != $new){
			$this-&gt;view-&gt;msg = "Please Confirm Password Correctly";
		}else{
			$arr = array(":idnumber" =&gt; $new, ":passwd" =&gt; $this-&gt;view-&gt;salt);
			$this-&gt;database-&gt;DBSet($arr,'users',$whr = 'WHERE ID = '.$add);
			$this-&gt;view-&gt;msg = "Password is Successfully Changed";
			$this-&gt;view-&gt;err = true;
		}
		
		$this-&gt;view-&gt;render('Account/ChangePasswordSubmit');
		
		
		
	}

	function AddItem(){
		$this-&gt;view-&gt;url	=	$this-&gt;config-&gt;url;
		$this-&gt;view-&gt;ID 	  = get_class($this);
		$this-&gt;view-&gt;Title = "D3 &gt; Account &gt; Add Item";
		$this-&gt;view-&gt;render('Account/AddItem');
	}

	function AddItemSubmit(){
		$this-&gt;view-&gt;url	=	$this-&gt;config-&gt;url;
		$this-&gt;view-&gt;ID 	  = get_class($this);
		$this-&gt;view-&gt;Title = "D3 &gt; Account &gt; Add Item";

		$itemName2 = isset($_POST['item_name']) ? ($_POST['item_name']) : '';
		$itemName = mysql_escape_string($itemName2);
		$img = isset($_POST['img_path']) ? ($_POST['img_path']) : '';
		$Quality = $_POST['quality'];
		$Type = $_POST['type'];
		$subType = $_POST['sub'][1];
		$TwoHand = $_POST['sub'][2];
		$Armor = $_POST['sub'][3];
		$itemAttrib1 = $_POST['list_1'];
		$itemAttrib2 = $_POST['list_2'];
		$itemAttrib3 = $_POST['list_3'];
		$itemAttrib4 = $_POST['list_4'];
		$itemAttrib5 = $_POST['list_5'];
		$itemAttrib6 = $_POST['list_6'];
		$itemAttrib7 = $_POST['list_7'];
		$itemAttrib8 = $_POST['list_8'];
		$Value_1 = isset($_POST['val_1']) ? ($_POST['val_1']) : '';
		$Value_2 = isset($_POST['val_2']) ? ($_POST['val_2']) : '';
		$Value_3 = isset($_POST['val_3']) ? ($_POST['val_3']) : '';
		$Value_4 = isset($_POST['val_4']) ? ($_POST['val_4']) : '';
		$Value_5 = isset($_POST['val_5']) ? ($_POST['val_5']) : '';
		$Value_6 = isset($_POST['val_6']) ? ($_POST['val_6']) : '';
		$Value_7 = isset($_POST['val_7']) ? ($_POST['val_7']) : '';
		$Value_8 = isset($_POST['val_8']) ? ($_POST['val_8']) : '';

		$sql = "SELECT * FROM item_images WHERE item_name = :imgurl ";
				$arr = array(":imgurl" =&gt; $itemName2);
				$ctr = $this-&gt;database-&gt;DBCtr($sql,$arr);
		if($ctr &gt; 0){
					$path = $this-&gt;database-&gt;DBQry($sql,$arr);
					$this-&gt;view-&gt;ipath = $path[0]['item_img'];
		}

		if ($subType == "All 1-Hand Item Types"){
			$Armor = $subType;
		}

		$ipath = $this-&gt;view-&gt;ipath;

		$this-&gt;view-&gt;msg = "";
		$this-&gt;view-&gt;err = false;

		if ($itemName == ""){
			$this-&gt;view-&gt;msg = "You must enter an item name!";
		}elseif ($Quality == "Any Quality"){
			$this-&gt;view-&gt;msg = "You must choose a quality!";
		}elseif ($Type == "Any Type"){
			$this-&gt;view-&gt;msg = "You must choose a type!";
		}elseif ($subType == "Any Subtype"){
			$this-&gt;view-&gt;msg = "You must choose a sub type!";
		}elseif (!ctype_digit($Value_1)){
			$this-&gt;view-&gt;msg = "You must enter a value";
		}else{
			$sql = "INSERT INTO `items_us_sc` (`item_num`, `item_name`, `quality`, `type`, `sub_type`, `attrib_1`, `value_1`, `attrib_2`, `value_2`, `attrib_3`, `value_3`, `attrib_4`, `value_4`, `attrib_5`, `value_5`, `attrib_6`, `value_6`, `attrib_7`, `value_7`, `attrib_8`, `value_8`, `item_img`) VALUES (NULL, '$itemName', '$Quality', '$Type', '$subType', '$itemAttrib1', '$Value_1', '$itemAttrib2', '$Value_2', '$itemAttrib3', '$Value_3', '$itemAttrib4', '$Value_4', '$itemAttrib5', '$Value_5', '$itemAttrib6', '$Value_6', '$itemAttrib7', '$Value_7', '$itemAttrib8', '$Value_8', '$ipath')";
			$arr = array();
			$ctr = $this-&gt;database-&gt;DBQry($sql,$arr);
			$this-&gt;view-&gt;msg = $subType;
			$this-&gt;view-&gt;err = true;
		}

		$this-&gt;view-&gt;render('Account/AddItemSubmit');

	}
}
?&gt;

You definitely need to add in those value attributes to the option tags. But what selections you make there don’t affect your code.

You need to check that you’re actually getting into AddItemSubmit(). Is the form submitting at all?

Perhaps consider using an existing plugin to achieve the chained select/linked list functionality.