list data from db and submit query?

Posted on 2004-11-24
Last Modified: 2008-02-26
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,


$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..
         echo "fault";
end code
Question by:dwax
    LVL 20

    Accepted Solution

    This sort of thing is usually done with JavaScript... of course if your data is too big for that to be feasible, then you should use JavaScript to update the contents of a hidden IFRAME...

    Expert Comment

    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.

    Expert Comment

    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.
    LVL 20

    Expert Comment

    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

    Expert Comment

    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.
    LVL 20

    Expert Comment

    with the iframe solution, you change the src as you change your text entry...

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

    <SCRIPT Language="JavaScript">
    myframe.src = "" + 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()



    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)

    Author Comment

    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!


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now