Link to home
Start Free TrialLog in
Avatar of dwax
dwax

asked on

list data from db and submit query?

hi everyone,

i am trying tot create the following, but i can't figure out how to do this.

A listbox (list/menu) gets names from db,
user selects a name from the list,
a query is send to db (where InputName=selected name),
output comes in texfields.

The problem for me is the following, how can i get the selected item from the list,
without using PHP_SELF?InputName=etc.

I've got something like this, but it has to function (if possible) without
getting the data from the browser...By the way, below code isn't working, anyway.

Is this possible? Anyone who kows how to this in a proper way?

Thanks in advance,

derek


code
----------------------------------------------------------
$result=mysql_query("select * from $dbTable3", $link);
$rowCheck = mysql_num_rows($result);

echo "<form name=\"CityName\" id=\"CityName\" action=\"$PHP_SELF\" method=\"get\">";
echo "<select name=\"InputName\" onchange=document.all.CityName.submit();>";
      
if($rowCheck > 0){

while($row = mysql_fetch_array($result)){

  //put in list

    echo "<option>".$row['Name']."</option>";
      }
      
      echo "</select>";

        $InputName = addslashes($_GET['InputName']);
      
      $result=mysql_query("select * from $dbTable3 where Name = '$InputName'", $link);
      
      // get data from db etc..
            
}else
 {
         echo "fault";
    }
----------------------------------------------------------------
end code
ASKER CERTIFIED SOLUTION
Avatar of virmaior
virmaior
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ypamudji
ypamudji

If the user selects a name from the list within the browser, how can you possibly get that data without going to the browser?  I'm confused.  Why do you have this requirement?

Typically in such a situation I would use Javscript's onChange to submit the data and reload the page with the new data passed along to PHP so it can handle the query and output the proper HTML.
Actually I just thought of a somewhat kludgy way.  The onChange, instead of submitting the data and having PHP reload the page with new info, could trigger a Javscript function.  Within that Javascript function you could already have a switch statement with all the corresponding data that you want outputted to the text field.  If the argument matches a case, that text is returned and displayed in your text box.  This solution is only feasible with small amounts of data though.  You don't want to have a gigantic Javascript function containing all your data if at all possible.
which is what i suggested in the first place...
the difficulties can be smoothed however by using a hidden iframe...
with this solution the javascript doesn't load the list, it loads the iframe which contains the list.  after it's done loading then the javascript repopulates the dropdown
Haven't had the chance to use iframe before, but wouldn't it be safer to use "object" instead, since it's part of the 4.0 standard?  Plus, wouldn't using iframe to store the data be the same as coding it directly into the Javascript function as far as the amount of data that needs to be delivered before the page loads?  How about some sample code or pseudocode to demonstrate how it would be beneficial?

Either way, I think your solution should work.
with the iframe solution, you change the src as you change your text entry...
e.g.

<INPUT Type="TEXT" OnChange="GetMyOptions" id="mytext" name="mytext">
<SELECT name="myselect">
</SELECT>

<SCRIPT Language="JavaScript">
myframe.src = "www.mypage.com/myopts.php?" + encode(document.getElementById("mytext").text);



  function TwoBoxMove(a,b,c)
    //Andrew Komasinski 2004-10-25 This will move the items from one box to the other
      {
           if (c == '>>') {
               var from = document.getElementById(a);
              var to = document.getElementById(b);
             } else
             {
               var from = document.getElementById(b);
              var to = document.getElementById(a);
             }
             while (from.selectedIndex > -1)
         {
     
              var lastoption = new Option();
          lastoption.text = from.options[from.selectedIndex].text;
          lastoption.value = from.options[from.selectedIndex].value;
              to.options[to.length] = lastoption;
              from.options[from.selectedIndex] = null;
             }
             
             
      }

function reloadQ()
{
   TwoBoxMove('myselect','shh','<<');
}

setTimeout('reloadQ();',1000*60*5);

</SCRIPT>

where shh is the name of the one being loaded in the iframe and has all of the options set to selected

(I use this code for some other stuff, edit it to fit your requirements exactly)
Avatar of dwax

ASKER

Thanks guys,

The reason why i the info not from the browser, is because i  have wuite a heavy
flashmovie on the same page. With every form submit, the movie loads again...

One possible solution, not very neat is put it into frames...

The other one is using an iframe as you suggested. I will look into that a.s.ap.

thanks for your replies!

derek