38 users online (0 members and 38 guests)  

Thread: Onclick Listbox


  Results 1 to 3 of 3

Related

  1. Onclick menu - Help    Forum: Javascript Forum
    Replies: 10
  2. Help me about Dropdown ListBox    Forum: HTML Forum
    Replies: 1
  3. onclick help    Forum: Javascript Forum
    Replies: 3
  4. onClick on php?    Forum: PHP Forum
    Replies: 1
  5. LISTBOX Width    Forum: HTML Forum
    Replies: 5
  1. #1
    Zoup's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2007
    Posts
    8

    Onclick Listbox

    Hello.

    I am an experienced programmer with lots of knowledge about alot but no master at anything.

    I am using PHP/MySQL and need to update a listbox2 based on what was just clicked on in listbox1 in real time no reloads.

    I was thinking about loading the possible results into an array and pulling them out as listbox1 selected but..

    pretty much i am clueless about this javascript.

    can anybody point me in the right direction?

  2. #2
    Zoup's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2007
    Posts
    8

    Re: Onclick Listbox

    I was able to figure it out with ajax.
    PHP Code:
    <script type="text/javascript">
    var request = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
    request = false;
    }
    }
    @end @*/
    if (!request && typeof XMLHttpRequest != 'undefined') {
    request = new XMLHttpRequest();
    }

    function ajaxSwitch(course) {
    var url = "cadenaselect2.php?course=" + escape(course);
    request.open("GET", url, true);
    request.onreadystatechange = go;
    request.send(null);
    }

    function go() {
    if (request.readyState == 4) {
    if (request.status == 200) {
    var response = request.responseText;
    var lista=document.getElementById("tee");
    var tees=response.split('|');
    for (i=1; i<tees.length; i++) {
        var x=document.createElement('option');
        var y=document.createTextNode(tees[i]);
        x.appendChild(y);
        lista.appendChild(x);
        }
       }
      }
    }

    function init() {
    var course=document.getElementById('course');
    course.onchange=function() {
        if(this.value!="") {
            var lista=document.getElementById("tee");
            while (lista.childNodes[0]) {lista.removeChild(lista.childNodes[0])}
            ajaxSwitch(this.value);
            }
        }
        ajaxSwitch(course.value);
    }
    window.onload=init;
    </script>
    <body> 
    <form method="get" action="#">
    <label>course: <select id="course" name="course">
    <?php
    include('../includes/db_login.php');

    $query "select * from course";
    $result mysql_query($query);

    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){
    echo 
    '<option>'.$row['course_name'].'</option>';
    }
    ?>
    </select></label>
    <label>tee: <select id="tee" name="tee"></select></label>
    </form>
    theres another form:

    PHP Code:
    function FindCourseTee($course) {
    switch (
    $course){
        case 
    "Golden Fox":
            return array(
    'Londres','Manchester','Birmingham','Liverpool','Leeds');
            break;
        case 
    "Estados Unidos":
            return array(
    'Washington DC','Nueva York','Los Angeles''Chicago');
            break;
        case 
    "Australia":
            return array(
    'Canberra','Melbourne','Sydney''Brisbane');
            break;
    }

    $course=@$_GET['course'];
    $tees=FindCourseTee($course);
    foreach (
    $tees as $tee) {
        echo 
    '|'.$tee;


  3. #3
    HTML's Avatar
    Administrator

    Status
    Offline
    Join Date
    Aug 2000
    Posts
    3,445

    Follow HTML On Twitter Add HTML on Facebook Add HTML on Google+ Add HTML on Linkedin Visit HTML's Youtube Channel

    Re: Onclick Listbox

    Glad you got it going, and thanks for helping others with their issues.

    D
    AHFBWEB Less customers per server, more power for you!

    Business Class Shared Hosting



Tags for this Thread