Forum Moderators: coopster

Message Too Old, No Replies

combo box item

         

ttkt

1:58 am on Jul 8, 2005 (gmt 0)

10+ Year Member



hi, i would like to retrieve the data from database and display into a combo box. the user then click on submit button when he has made a selection. the result table will be displayed after that. how do i retrieve the selected item in the combo box so that i can make an sql query where the columnName = comboBox.selectedItem and display the related results on the table?

below is the function where i display data in combo box

function selectEquipType()
{
if (!is_resource($this->mysql_resource_combo))
die ("User doesn't supply any valid mysql resource after executing query result". mysql_error());

// load data into combo box

echo "<form name = \"queryEquipTypeForm\" method = \"post\" action = \"equip_onLoan.php?typeID = $typeID\">";
echo "<select name=\"equipTypeCombo\">\n";

//Now fill the table with data
while ($row = mysql_fetch_array($this->mysql_resource_combo))
{
$type = $row['description'];
$typeID = $row['assetTypeID'];
echo "<option value <?php $typeID?>$type</option>\n";
}
echo "</select>\n";
echo "<input type=\"submit\" value=\"Query\" name=\"Query\" onsubmit=\"searchEquipType(\"queryEquipTypeForm\", \"equipTypeCombo\")\">";

return $eTypeID;
}

Blackie

7:06 am on Jul 8, 2005 (gmt 0)

10+ Year Member



You simply use $equipTypeCombo.

ergophobe

5:13 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If, on the other hand, register_globals is off, you would instead use

$_POST['equipTypeCombo']

ttkt

5:09 pm on Jul 9, 2005 (gmt 0)

10+ Year Member



i have inserted both $ into my codes but i tried to print the result to see if it has been captured but was empty.

i have this piece of code on one php page:

function selectEquipType()
{
if (!is_resource($this->mysql_resource_combo))
die ("User doesn't supply any valid mysql resource after executing query result". mysql_error());

// load data into combo box

echo "<form name = \"queryEquipTypeForm\" method = \"post\" action = \"equip_onLoan.php\">";
echo "<select name=\"equipTypeCombo\">\n";

//Now fill the table with data
while ($row = mysql_fetch_array($this->mysql_resource_combo))
{
$type = $row['description'];
$typeID = $row['assetTypeID'];
echo "<option value <?php $typeID?>$type</option>\n";
}
echo "</select>\n";
echo "<input type=\"submit\" value=\"Query\" name=\"Query\">";
echo "combo box data is ". $_POST['equipTypeCombo'];

return $_POST['equipTypeCombo'] ;

whereas this piece of code on another php page (equip_onLoan.php):

// display all the Equipment type onto combox
$res_combo = mysql_query("select * from ims_assetType order by description asc");
$prg->mysql_resource_combo = $res_combo;
$eType = $prg->selectEquipType();
echo "combo box data is in equip on loan page". $eType;

i would like the selected item in combo box to be captured and passed into sql statement to retrieve the data. how do i go about it, thanks.

ergophobe

5:54 pm on Jul 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you submitting the page?

This code is not necessary:


echo "combo box data is ". $_POST['equipTypeCombo'];

return $_POST['equipTypeCombo'] ;

There is no reason to return anything from the $_POST array as it is a superglobal [us2.php.net] and is therefore automatically available outside the function without returning the value.

On the first time the page is loaded, if you set your error reporting to E_ALL this should be giving you a notice for that echo statement and the return statement (undefined index).

If you don't have your error_reporting set to E_ALL, add this line at the top of the script

error_reporting(E_ALL);

ttkt

6:08 pm on Jul 9, 2005 (gmt 0)

10+ Year Member



hi, yes, i am submitting the page to equip_onLoan.php where the sql statements are there. then i'll do a concat to the sql statement where type = option selected in combo box

ttkt

6:19 pm on Jul 9, 2005 (gmt 0)

10+ Year Member



i have insert error_reporting(E_ALL); into the page.

yes, it says undefined index when the page is first loaded.

ergophobe

7:01 pm on Jul 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



to get the item into your SQL, you have to put in in your query after the page is submitted (i.e. no "undefined index" notice).

$query = "SELECT * FROM mytable WHERE myfield='" . $_POST['equipTypeOption'] . "'";

ttkt

3:37 pm on Jul 11, 2005 (gmt 0)

10+ Year Member



yup, it works..thanks!

but there is one strange thing...e.g. the item "voice recorder" is selected inside the combo box. $_POST[equipTypeCombo] only retrieves "voice" but not "voice recorder". any idea why is this so? the same goes to other string when there is a space in between...like e.g. digital camera

ergophobe

8:27 pm on Jul 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Been away for a few days, sorry.

I'm not sure what you mean there. You mean when someone selects "voice recorder" and submits, you only get "voice"?

It probably would have to do with quoting your attributes in the HTML then or something like that.