SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member meow's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    query dropdown menu value

    Hi..

    I have a dropdown menu thats pull value from MySQL.
    Now i want to query the selected value from dd menu so then it will display a list that match with it in database without submit the page.
    Do i have to use onChange function and how?
    Sorry i'm new to javascript.

    Thanks in advance.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure I get what you are asking. Can you be more specific, maybe a code snippet to help us understand?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Member meow's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok My problem is something like this.

    This is my dd menu. (Actually its pullout data from MySQL)
    PHP Code:
    <select name="semester" size="1">
      <
    option value="1">1</option>
      <
    option value="2">2</option>
      <
    option value="3">3</option>
      <
    option value="4">4</option>
    </
    select
    1. Let say user select 1.
    2. Query the database based on selected value.
    $query = "SELECT * FROM semester WHERE sem = '1'"
    3. Then it will display a result that match with the query without submit the page.

    Any ideas?
    Please help.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you couldn't acutally do this w/o submitting the page. However, if you devised an algorithm to stuff all the results from SELECT * FROM semester into a javascript array, then you can use the onChange() event attached to the SELECT to display what you need. Something like this perhaps
    PHP Code:
    $result mysql_query("SELECT * FROM semester");
    $fetch mysql_fetch_array($result);
    echo(
    "<script>\n var arr = new Array();\n");
    $count 0;
    foreach(
    $fetch as $val) {
        echo(
    "arr[".$count."] = '".$val."\n");
        
    count++;
        }
    echo(
    "</script>\n"); 
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Member meow's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    I've tried that one but it doesnt work.
    I'm more into php rather than javascript.

    Is there any other way?

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm, maybe a bit more like this...
    PHP Code:
    $result mysql_query("SELECT * FROM semester");
    echo(
    "<script>\n var arr = new Array();\n");
    $countOuter 0;
    $countInner 0;
    foreach (
    mysql_fetch_array($result) as $tempArr)
        {
        echo(
    "arr[".$countOuter."] = new Array()\n");
        
    $countInner 0;
        foreach(
    $tempArr as $val
            {
            echo(
    "arr[".$countOuter."][".$countInner."] = '".$val."\n");
            
    $countInner++;
            }
        
    $countOuter++;    
        }

    echo(
    "</script>\n"); 
    This may need a little debugging, but that *SHOULD* populate your array properly. If it does, let me know, and we can move on the using the SELECT to switch it all
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Member meow's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. with changes at
    foreach($tempArr as $val) to
    foreach(mysql_fetch_array($result) as $val) it will give me array like this
    arr[0] = new Array()
    arr[0][0] = '2
    arr[0][1] = '2
    arr[0][2] = '2

    This array give redundant data. One silly question from me is how the array will looklike?

    Thanks.

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This worked for me

    http://www.lanwizards.com/test.php

    In put the PHP source on the page in HTML. You can View>>Source to see the javascript array.

    My test semesters table looks like this
    Code:
    +----+--------+----------+
    | id |  name  |  length  |
    +----+--------+----------+
    | 1  | Fall   | 3 months |
    | 2  | Winter | 3 months |
    | 3  | Spring | 3 months |
    | 4  | Summer | 1 month  |
    +----+--------+----------+
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    SitePoint Member meow's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okies
    Thanks I got it.
    Whats next?

  10. #10
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Well, you couldn't acutally do this w/o submitting the page", this is possible with remote scripting or using the xml dom.

    heres an example of this

    http://www.ashleyit.com/rs/jsrs/select/php/select.php

    also
    http://www.w3schools.com/xml/xml_http.asp

    any way try that


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •