...

View Full Version : Unable to get Select Tag value (But input class works)



Shogikishi
05-09-2008, 01:27 AM
Hi,

Can someone be the expert I need and help me see why I can't get this value to pass in PHP?

I have a PHP page with three DIVs. On page load, only the first DIV (DIV#1) has controls. The first DIV (DIV#1) contains a button that, when clicked, generates a control and button in DIV#2. The intent is to have a user select a value from a list box in DIV#2 and then click an action button also in DIV#2. The resulting action should be for the value selected in the listbox of DIV#2 to be displayed in DIV#3. I am using AjaxCore/Prototype.js for AJAX like effects.

The listbox is displayed and appears ok, but I cannot get a value from the listbox (Select Tag). When I use Select Tag the button passes an empty variable.

When I swap out the Select Tag section for a simple Input Class=textfield, then it all works! Please see code snippet below.

How do I get this working with a listbox or something like it?

-Shogikishi



###DIV#1 button has onclick function to Workspace1(). This is where the controls are created as text and inserted into DIV#2 as innerHTML.

###The button in DIV#2 has onclick function to ChartType1(). When a InputClass of TextField is used, then it prints the value put in the textbox. When a Select Tag is used instead, it doesn't print the value.


###################
public function Workspace1()
{
$this->Workspace1Controls();
}
# populate workspace1
private function Workspace1Controls()
//onMouseOver="alertselected()"
{
$var = (!empty($_REQUEST['selectSY']))?$_REQUEST['selectSY']:'';
//$html .= '<form id="ChartType1Controls" name="ChartType1Controls" method="post" >';
$html .= '<label for="selectSY">School Year </label><br/>';

#############################
# Want it as a listbox
##############################

$html .= '<select name="selectSY" id="selectSY">
<option value="'.$var.'">2003 - 2004</option>
<option value="'.$var.'">2004 - 2005</option>
<option value="'.$var.'">2005 - 2006</option>
<option value="'.$var.'">2006 - 2007</option>
<option value="'.$var.'" selected="selected">2007 - 2008</option></select>';

###########################
# Try it as a text input field and it works!
############################
//$html .= '<input class="textfield" type="text" name="selectSY" id="selectSY" value="'.$var.'" />';

##################
$html .= '<input class="btnfield" type="button" id="chart1_btn" name="chart1_btn" value="Create Chart" onclick="'.$this->bindInline("ChartType1", "selectSY").'"/>';
$html .= '</form>';

//echo $this->htmlInner("controls",$html);
echo $this->htmlInner("controls",$html);

}




####################################
# START PULL CHART TYPE 1
#######################################
public function ChartType1()
{

//selectedItem = this.ChartType1Controls.selectSY.selectedIndex;
//selectedValue = this.ChartType1Controls.selectSY.options[selectedItem].value;
$var = (!empty($_REQUEST['selectSY']))?$_REQUEST['selectSY']:'';

if ($var=="")
{
echo "The variable container is empty";
}

$SY = $var;

echo $SY." is the value";

//echo include("../Chart1/Index.php");
//echo include_once("../Chart1/Index.php");

}

Shogikishi
05-09-2008, 01:40 AM
I have stumbled (it really feels like I've bumbled my way through this over the last couple days) across a partial fix. Replacing "<option value="'.$var.'" " with "<option value=" and an integer, such as 1, gets the function working. The exception is when the option value is =0. When "=0" and the user selects that value, the function issues an empty variable. So "0" is not passing through. Any ideas?

Shogikishi
05-09-2008, 01:46 AM
As a temporary fix I have added an if statement to catch an empty variable (caused by 'option value=0'). It fills an empty variable with "0" and the function executes as intended.

Why wouldn't <option value="0"> work?

#####
Updated ChartType1() function
#####
public function ChartType1()
{

//selectedItem = this.ChartType1Controls.selectSY.selectedIndex;
//selectedValue = this.ChartType1Controls.selectSY.options[selectedItem].value;
$var = (!empty($_REQUEST['selectSY']))?$_REQUEST['selectSY']:'';

if ($var=="")
{
$var=0;

} else {
$var = $var;
}
$SY = $var;

echo $SY." is the value";
echo include("../Chart1/Index.php");
//echo include_once("../Chart1/Index.php");

}

tomws
05-09-2008, 03:49 AM
Why wouldn't <option value="0"> work?

Because of this line:

$var = (!empty($_REQUEST['selectSY']))?$_REQUEST['selectSY']:'';

Unless I'm missing something, that line is essentially meaningless, besides stripping that zero. From the php manual for empty (http://us3.php.net/empty):

* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

So, that line is saying: "If the request var is not empty, set $var to it. Otherwise, set it to empty." That's why I say it's meaningless.

By the way, one of the top stickies in the forum says (with good reason) to wrap your code in appropriate tags - in the subject line, even. Really helps readability, which means it helps get responses, too.

Shogikishi
05-13-2008, 06:20 PM
Thanks tomws. I'll look for the appropriate tags and use them next time.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum