Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthormaweber98
    • CommentTimeOct 9th 2007
     permalink
    I have followed the example at w3schools to use AJAX to dynamically connect to a mysql database using a PHP script and bring the content back to the page. I have everything set up in the example link below. My problem is that I want to do two seperate queries on a form and bring the information back to the page. The queries both work but the placement of the information is giving me problems. It places the results of both queries in the same <div> container. Even though the <div> containers have different ID's.


    http://www.coastalpet.com/testing/product_admin_st4.php

    The javascript cod is as follows:

    var xmlHttp

    function showColor(str)
    {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request")
    return
    }
    //var url="getcustomer.asp"
    var url="getcolor.php?ID="
    url=url+""+str
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }

    function stateChanged()
    {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById("txtColor").innerHTML=xmlHttp.responseText
    }
    }

    function GetXmlHttpObject()
    {
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
    objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
    }

    Does anyone have any suggestions on how to get the results in the right spots on the page. Thank you!
    • CommentAuthordavist11
    • CommentTimeOct 9th 2007
     permalink
    Thats weird that its doing that. My recommendation would be to use a JS library to handle the ajax stuff instead of recreating it for each ajax call. Try the jquery ajax functions: http://docs.jquery.com/Ajax
  1.  permalink

    That's because you have defined the stateChanged() function twice.

    First time in here: <script src="selectcolor.js"></script> and the second time in here: <script src="selectdescription.js"></script>

    Renaming them to colorStateChanged and descriptionStateChanged, or something, should do the trick, but there's still more work to be done, as you seem to be doing everything two times. If you passed the name of the required PHP file as a parameter to your show function, you could trim down a lot of unnecessary repetition.

    • CommentAuthormaweber98
    • CommentTimeOct 9th 2007
     permalink
    kari.patila,
    Thank you for your help. I was able to rename the StateChanged function so they didn't conflict and it corrected the problem. I think I would be better off passing a parameter to the function like you mentioned. Could you point me in the right direction on how to pass a parameter into a function?
  2.  permalink

    There are better ways to do this, but you could get rid of the other js file, replace the function showColor(str) line with

    function showColor(str, getFile)

    and then replace the line

    var url="getcolor.php?ID="

    with

    var url=getFile+"?ID="

    and then finally change the function calls to

    showColor(this.value, 'filename.php')

    Now, does this actually work? I have no idea, but you should get the point, at the very least.

    • CommentAuthormaweber98
    • CommentTimeOct 11th 2007
     permalink
    It worked so far. But I'm not sure how to pass the "div id" variable to the js file. Can anybody help with this. Thank you!
    // JavaScript Document
    var xmlHttp

    function showColor(str, getFile)
    {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request")
    return
    }

    var url=getFile+"?ID="
    url=url+""+str
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=DescriptionstateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }

    function DescriptionstateChanged()
    {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById().innerHTML=xmlHttp.responseText
    }
    }

    function GetXmlHttpObject()
    {
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
    objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
    }
  3.  permalink

    How about making these changes to the JS

    function showColor(str, getFile, div)

    xmlHttp.onreadystatechange=DescriptionstateChanged(div)

    function DescriptionstateChanged(div)

    document.getElementById(div).innerHTML=xmlHttp.responseText

    and this one to the HTML

    showColor(this.value, 'filename.php', 'txtColor')

    • CommentAuthormaweber98
    • CommentTimeOct 12th 2007
     permalink
    I made the changes and now I'm getting an error in IE that says "Type Mismatch". Firefox does not show any errors but neither browser brings back the results. I have posted the js file. Any ideas as to why this is not working. I'm stumped. Thankyou!

    // JavaScript Document
    var xmlHttp

    function showColor(str, getFile, div)
    {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request")
    return
    }

    var url=getFile+"?ID="
    url=url+""+str
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=DescriptionstateChanged(div)
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }

    function DescriptionstateChanged(div)
    {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById(div).innerHTML=xmlHttp.responseText
    }
    }

    function GetXmlHttpObject()
    {
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
    objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
    }

    --- HTML ---

    onchange="showColor(this.value, 'getcolor.php','txtColor')"

    <div id="txtColor">
    •  
      CommentAuthorkari.patila
    • CommentTimeOct 12th 2007 edited
     permalink

    Sorry about that. Try

    xmlHttp.onreadystatechange=function() { DescriptionstateChanged(div); };

    instead.

    • CommentAuthormaweber98
    • CommentTimeOct 12th 2007
     permalink
    brillant! It works perfectly. Thank you!
Add your comments
    Username Password
  • Format comments as (Help)