Simple JS/AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hubson
    New Member
    • Aug 2006
    • 3

    Simple JS/AJAX

    Hello Evrybody.I wrote this page:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html><head>
    <meta http-equiv="Content-type" content="text/html;charset=IS O-8859-2">
    <meta name="Author" content="Hubert Czlapinski">
    <meta http-equiv="Reply-to" content="">
    <meta name="Revisit-after" content="30 days">
    <title></title>

    <script language = "javascript ">
    var http_request = false;
    if (window.XMLHttp Request) {
    http_request = new XMLHttpRequest( );
    }else if (window.ActiveX Object) {
    http_request = new ActiveXObject(" Microsoft.XMLHT TP");
    }

    function getData() {

    if(http_request ) {
    var obj_id = document.getEle mentById('divId ');
    var color = document.getEle mentById('selec t').value;
    var url = "colors.php?col ors="+color;
    http_request.op en("GET", url);
    http_request.on readystatechang e = function(){

    if (http_request.r eadyState == 4 && http_request.st atus == 200) {
    obj_id.innerHTM L = responseText;;
    }

    }


    http_request.se nd(null);
    }
    }

    </script>
    </head>

    <body>

    <select id="select">
    <option>Red</option>
    <option>Black </option>
    <option>Grey</option>
    </select>
    <input type="button" onclick="getDat a()" value="Send request"/>
    <div id="divId">The text should apear here.</div>

    </body>
    </html>

    my colors.php looks like:


    <?php


    if($_GET['colors']=="Red"){

    echo "The color u`ve chosen was red";

    }elseif($_GET['colors']=="Black"){

    echo "The color u`ve chosen was black";

    }elseif($_GET['colors']=="Grey"){

    echo "The color u`ve chosen was grey";
    }
    else echo "I didn`t catch the request.";

    ?>
    The php responses as if the select value wasnt in the url?
    How can i set the color var to what i select .
  • Hubson
    New Member
    • Aug 2006
    • 3

    #2
    there should be "obj_id.innerHT ML = http_request.re sponseText;"-but its still not working.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Have you checked that the url string is correct by "alerting" before sending.

      I do not think that this

      var color = document.getEle mentById('selec t').value;

      is working as you think because you have not given any of you option tags a value (the text is not the value there is a value attribute).

      Comment

      • dennischikwayi
        New Member
        • Aug 2006
        • 2

        #4
        Originally posted by Banfa
        Have you checked that the url string is correct by "alerting" before sending.

        I do not think that this

        var color = document.getEle mentById('selec t').value;

        is working as you think because you have not given any of you option tags a value (the text is not the value there is a value attribute).
        for u to get the color use the code below

        shld put values in the options and use the code belo

        var color = document.getEle mentById('selec t').options[document.getEle mentById('selec t').selectedInd ex].value;

        alternatively if u had passed the select object u would use

        document.obj.op tions[obj.selectedInd ex].value but u as well had to put values in the options

        Comment

        Working...