Ajax Menu

Discussion in 'JavaScript' started by adamjblakey, Dec 5, 2010.

  1. #1
    Hi,

    I have the following function which fills a text field depending on what is entered in a previous field. However i want to change this slightly so that a menu is filled rather than a textfield.

    What do i need change below:

    
    function showHint(str)
    {
    if (str.length==0)
      { 
      document.getElementById("txtHint").innerHTML="";
      return;
      }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
    	document.register_account.university.value = xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","update-university.php?q="+str,true);
    xmlhttp.send();
    }
    
    Code (markup):
    Cheers,
    Adam
     
    adamjblakey, Dec 5, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Last edited: Dec 5, 2010
    tvoodoo, Dec 5, 2010 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks but i think the above is working fine if this change can be made.
     
    adamjblakey, Dec 6, 2010 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    what do you mean by menu? do you mean a drop down box? or a list?
     
    camjohnson95, Dec 6, 2010 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Sorry, yes i meant to say list.
     
    adamjblakey, Dec 6, 2010 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Well you need to change the server-side page: update-univerity.php to output the list as html (e.g <ul><li> etc.) then just change:
    document.register_account.university.value = xmlhttp.responseText;
    to:
    document.getElementById("listdiv").innerHTML = xmlhttp.responseText;
    and add a div to the html of the page (where you want the list to appear) :
    <div id="listdiv"></div>
     
    camjohnson95, Dec 6, 2010 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    I am really sorry about this but i am an idiot and meant to say drop down select menu. Any chance you could have another look at it?
     
    adamjblakey, Dec 7, 2010 IP
  8. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #8
    Do in your php the return values as :
    <select value="the_value">the_name</select>

    and afterwards use : document.getElementById("selectid").innerHTML = xmlhttp.responseText;
     
    tvoodoo, Dec 7, 2010 IP
  9. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #9
    Great stuff thanks a lot.
     
    adamjblakey, Dec 7, 2010 IP