Quickform Hierselect

I am trying to use the hierselect in a Quickform. I believe all values have been added to the form successfully, but i’m unable to retrieve the selected values.

Here is my code:

// Include QuickForm class 
require_once 'HTML/QuickForm.php'; 
// Default URL: usually index.php 
$target = 'index.php'; 
$form = new HTML_QuickForm('addForm', 'POST', $target); 

// Add a header to the form 
$form->addElement('header', 'MyHeader', 'Please Enter Details');    

$form->addElement('text', 'productsname', 'Product Name'); 
$form->addRule('productsname', 'Enter Product Name', 'required', false, 'client'); 
$form->addElement('text', 'productsstyleno', 'Product Style No'); 
$form->addRule('productsstyleno', 'Enter Product Style No', 'required', false, 'client'); 
$form->addElement('text', 'productsprice', 'Product Price'); 
$form->addRule('productsprice', 'Price must be numeric', 'numeric', false, 'client'); 
$form->addRule('productsprice', 'Enter Product Price', 'required', false, 'client'); 
$form->addElement('text', 'productsfabric', 'Product Fabric'); 
$form->addElement('select', 'productscolour', 'Product Colour'); 


// create level 0 and level 1 arrays 
// from database queries 
try { 
    foreach ($db->query("SELECT category_id, category_name from tbl_categories WHERE category_parent = '43'") as $row) { 
        $id = $row['category_id']; 
        $name = $row['category_name']; 
        $categories[$id] = $name; 
        foreach ($db->query("SELECT category_id, category_name from tbl_categories WHERE category_parent = '$id'") as $row2) { 
            $cid = $row2['category_id']; 
            $cname = $row2['category_name']; 
            $subcategories[$id][$cid] = $cname; 
        }        
    } 
    unset($db); 
} catch (PDOException $e) { 
    die ($e->getMessage()); 
} 

// add hierselect element 
$select = $form->addElement('hierselect', 'category', 'Category'); 
$select->setOptions(array($categories, $subcategories)); 
// Add a submit button 
$form->addElement('submit', 'submit', ' Submit '); 

I am using this to try and access the category Hierselect, but it just returns ‘Array’:

// print submitted values 
// render and display the form 
if ($form->validate()) { 
  $form->freeze(); 
  echo 'You submitted the following data:'; 
  $values = $form->getSubmitValues(); 
  echo $values; 
} else { 
  $form->display(); 
}

I’ve been tearing my hair out for 3 hours now, any help much appreciated.

Cheers,
Tim

Is there anyone else out there that knows anything about hierselect in quickform?

As far as all the documentation goes, this code should get me the selected values, but it doesn’t (see above):

  $values = $form->getSubmitValues(); 
  $disp = $values["category"];
  echo "DISP = " . $disp;

Thanks everyone

The onchange function is created automatically by Quickform. I believe it is necessary to change the 2nd select box when the first is altered.

Try once :


print_r($_POST);

and see whether it has the category field or not. Of course you make sure first you have selected the categories correctly.

Thanks very much for your reply, but I already tried that. Here is what gets output to the browser:

You submitted the following data:Array ( [productsname] => Jumper [productsstyleno] => dsds [productsprice] => 12 [productsfabric] => 90% Nylon [category] => Array [submit] => Submit )

It still churns out “Array”…

I’m sure the solution is out there, but I couldn’t find it after hours of searching last night…

If you try to echo an array, it will simply output “Array”.

instead, try print_r($values) and you will get all the content.


// print submitted values 
// render and display the form 
if ($form->validate()) { 
  $form->freeze(); 
  echo 'You submitted the following data:'; 
  $values = $form->getSubmitValues(); 
  print_r($values); // output the submitted form values
} else { 
  $form->display(); 
}

BTW can we see your generate whole form once?

That isn’t possible. The Hierselect element within Quickform generates them automatically.

Someone on here must have used this before? Anyone?

What does the “onchange” function do, and is it absolutely necessary for you to have it?


<select name="category[0]" onchange="_hs_swapOptions(this.form, 'category', 0);"> 

I’m sorry, that doesn’t make any sense to me.

Any idea what I have done wrong? How do I get at the option values in the 2nd select?

BTW, I can see that the select boxes have been filled out correctly by viewing the source:

<select name="category[0]" onchange="_hs_swapOptions(this.form, 'category', 0);"> 
	<option value="44">Ladies</option> 
	<option value="45">Miniatures</option> 
</select>&nbsp;<select name="category[1]"> 
	<option value="46">Tops</option> 
	<option value="47">Skirts</option> 
	<option value="48">Dresses</option> 
	<option value="49">Jackets & Coats</option> 
	<option value="50">Knitwear</option> 
	<option value="51">Trousers</option> 
	<option value="52">Shoes</option> 
	<option value="53">Accessories</option> 
	<option value="54">Nightwear & Underwear</option> 
</select>

Can anyone help? Please

Have you considered change the names of your selects, so you dont have two category?
It should be possible to do it with something like “Topcat” and “subcat”?

Since its the form arrays which are problematic, it could be worth changeing the names, atleast for some debugging…

I am afraid if the HTML output in the page is exactly same what you have mentioned above and the categories elements are inside the same form :stuck_out_tongue:

Hi rajug,

I’m afraid that outputs exactly the same:

You submitted the following data:Array ( [productsname] => Jumper [productsstyleno] => dsds [productsprice] => 76 [productsfabric] => 90% Nylon [category] => Array [submit] => Submit )

Anything else?