Need to use array from Head to populate drop down

kallym 17 Apr, 2009
Hi,
I've searched the forum, but can't find the answer, so hope someone can help. I am populating a drop-down box from an array that is in an include file in the head of my index.php page. I have a non-Joomla/non-Chronoforms version of the form working fine. But, I can't get the the code to work in Chronoforms.

My problem seems to be that the array and other variables used elsewhere on the page are not recognized in the Chronoforms section. The array and variables in question are not created within a function, so am not sure why this is. So, assuming Chronoforms is in a function, I tried global, but that didn't work either.

I tried reset(), but get this message: Passed variable is not an array or object .

If I include the array file and set the variables again in the Chronoforms form code, it will work, but I would like to do this without duplicating code.

In the example below you will see that I have included my array file, so the array is being seen, but the variable $thisStyle is not recognized.

Another problem is that the second drop down box is not being populated when the page loads, but is being populated when the first drop-down is changed (as a result of an onChange function). Plus there is a thumbnail image which should change when the color is changed that is not working, but again, that variable is set (and updated) in the head via ajax, so I imagine these are both similar problem with variables not being recognized.

Again, I have a copy of the form that is fully functional. It's just not quite there yet in Chronoforms.

  <div class="form_element cf_dropdown">
    <label class="cf_label">Style</label> 
<?php 
include('inc/array.php');
  //print_r($qsStyles);
$count = count($qsStyles);
 ?>
    <select class="cf_inputbox validate-selection" id="select_2" size="1" title="You must select a Style" name="qs_style" onchange="getThemesList(this,'select_3')">
<option value=""> -- select a style --</option>
<?php
for ($i=1; $i<=$count; $i++){ echo '<option value="'.$i.'"';
  if($qsStyles[$i] == $thisStyle) { echo ' selected="selected"'; }
  echo '>'. $qsStyles[$i] .'</option>
" ';
echo "\n";	
}
?>
    </select>    
  </div>

<div class="form_element cf_dropdown">
    <label class="cf_label">Color</label>
    <select class="cf_inputbox validate-selection" id="select_3" size="1" title="" name="color" onchange="getNewThumb(this,'select_2','ChronoContact_qsstyles')">
<option value=""> -- select a color --</option>
<?php if(isset($thisColor) && isset($thisStyle)){ 			
	$i=1;
	$counter = 1;    
	while ($counter <= $number_of_colors) { 
              echo '<option value="'.$i.'"';
			  if($i == $thisColor) { echo ' selected="selected"'; }
			  echo '>'.$thisStyle."-".$i.'</option>';			  
  			$counter++;
             $i++;
         }
		 } ?>
</select>
  </div>




Thank you,
Kally
GreyHead 17 Apr, 2009
Hi Kally,

I suspect that the first part of the answer is that you don't have a full path for the include file?
include('inc/array.php');

I think that this will be treated as a relative folder so probably it will be looking for components/com_chronocontact/inc/array.php and I suspect that isn't where the file is.

I find it safer to use a full path
require_once(JPATH_SITE.DS.'inc'.DS.'array.php');
assuming that the inc folder is in the site root.

Let's see if this also solves the other problems.

Bob
kallym 17 Apr, 2009
Thanks for your quick reply. Unfortunately that didn't work - or should I say it works the same, makes no difference. I did get everything working on the second drop-down and the image once I set the two variable below (which are also set in the head).

The following code works, my drop downs are populating and the onChange functions are working, but I am still including the array file twice and setting the $thisStyle and $thisColor variables twice as I need those elsewhere on the page. Adding JPATH_SITE.DS to the include path works the same either way, but I am glad to have that code and will use it in the future. Let me know if you have any other ideas.

Thank you!

<?php 
  include(JPATH_SITE.DS.'inc/array.php');
  //print_r($qsStyles);
  if(isset($_REQUEST['qs'])){ $thisStyle = $_REQUEST['qs']; } 
  if(isset($_REQUEST['qt'])){ $thisColor = $_REQUEST['qt']; } 
  //global $thisStyle;
  //global $thisColor;

	$count = count($qsStyles);
 ?>
<select class="cf_inputbox validate-selection" id="select_2" size="1" title="You must select a Style" name="qsstyle" onchange="getColorsList(this)">
<option value=""> -- select a Style --</option>
<?php
for ($i=1; $i<=$count; $i++){ echo '<option value="'.$i.'"';
  if($qsStyles[$i] == $thisStyle) { echo ' selected="selected"'; }
  echo '>'. $qsStyles[$i] .'</option>
" ';
echo "\n";	
}
?>
    </select>  
GreyHead 18 Apr, 2009
Hi kallym,

Files loaded in the head of your index.php file are most likely not available when the PHP for ChronoForms is executed. Using require_once will make sure that they are not loaded twice. If they are loaded then you may still need to make sure that your variables are available in the scope of the ChronoForms classes/functions. Try declaring them as global in both places and see if that helps.

Bob
kallym 26 Apr, 2009
Thanks for your help. I tried using global in both places, but that seemed to break both. I'll try again when I have some time to review the whole script and index template with a clear mind. For now, I have it working by including the array.php page in both the index template and in the chronoForms code for the form. There are actually going to be a few forms and a different array.php file included for each one. I really dislike redundant code, so when I have them all working I may go back and test again.

Thanks again, you do a great work for the people on this board!
This topic is locked and no more replies can be posted.