Jump to content


Photo

HOW TO RETRIEVE & DISPLAY STORED DATABASE VALUES IN LISTBOX AS SELECTED VALUES


  • Please log in to reply
2 replies to this topic

#1 sunnypal

sunnypal

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 11 September 2009 - 03:35 PM

Hello,
We are working on a form that has bunch of list boxes that display gender, ethnicity, student type values, etc in each of the list boxes.
After the form is submitted these list box values are stored in a different tables.

I am now working on retrieving and displaying values that have been inserted. We are able to retrieve and display single values (such as text boxes)
but we are stumped with list boxes. We need to retrieve all the values for each list boxes that we previously inserted and then display these values as selected(higlhlighted) in the list box. We are using a combination of AJAX, javascript and php (to retrieve the db values).

Has any one worked on list box values and their retrievals from the database.

Any help would be appreciated.

thanks,

#2 holiks

holiks

    Advanced Member

  • Members
  • PipPipPip
  • 83 posts

Posted 11 September 2009 - 03:42 PM

Someone else may care to elaborate a bit more...but I think I remember a while ago doing something like looping through the result set instead of retrieving "just one" and building the list that way. If a value is tagged as "selected" then tack on "selected" in the generated select_list_html. And there you have it....selected too. HTH ...even a bit
__________________________________________________________[br]$php = array('..a stack', '...once you pop', '...you don't stop');

#3 sunnypal

sunnypal

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 11 September 2009 - 05:53 PM


I am able to retrieve the values in the listbox. But this is the scenario when one wants to update a record.
The form (details.php) is run by a unique code, when the code is typed in onblur event  triggers a function:
getMSSQLData('find_mssqldata.php?schName='+document.inventoryForm.schName.value) where schName is the code that is passed to the find_mssqldata.php,
the find_mssqldata.php retrieves all the values for that code below:

find_mssql_data.php:
<?php

$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

$schName=$_REQUEST['schName'];


  $query = "select DISTINCT ETH from SD_ETH WHERE ETH_DTL_CD = '$schName'
  AND ETH_SEQ_NBR = (SELECT MAX(I.ETH_SEQ_NBR) FROM SD_ETH I WHERE I. ETH_DTL_CD = '$schName')";

$result = mssql_query($query); 

//$row = array();
  while ($row = mssql_fetch_array($result,MSSQL_BOTH)) {
  echo  $row['ETH'];
echo  "|";
    }

//echo "Scholar detail transaction complete";
mssql_close($dbhandle);



?>

Next, in the details.php I have AJAX function calls to retrieve the values returned by find_mssqldata.php


function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}

return xmlhttp;
}

but this is where it doesnt assign as selected values that are returned by find_mssqldata.php
Below I tried testing assigned the very first value  (mySplitResult[0]) as selected value but it keeps looping.
I need a way to store all returned values first and then assign them as selected values.

function getMSSQLData(strURL)
{
alert ("test");

var req = getXMLHTTP();
if (req)
{
//function to be called when state is changed
req.onreadystatechange = function()
{
//when state is completed i.e 4
if (req.readyState == 4)
{
// only if http status is "OK"
if (req.status == 200)
{ //alert ("test2");
//document.getElementById('Sname').value=req.responseText;
var mySplitResult = req.responseText.split("|"); //updated
alert(mySplitResult[0]);
if(mySplitResult[0] == undefined){

document.inventoryForm.schType.options[document.inventoryForm.criEth.selectedIndex].text='';
document.getElementById('criCountry').focus(); } else {
alert("k");
    var k=0;
var index =-1;
do {
    if (mySplitResult[0] == document.inventoryForm.criEth[k].value) {
index = k;
alert("index");
break;
}
k = k + 1;
} while (k < 2);//document.inventoryForm.criEth.length);
document.inventoryForm.criEth[index].selected=true;

}
document.inventoryForm.criEth[0].selected=true;

}
else
{
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}

req.open("GET", strURL, true);
req.send(null);
}
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com