Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Location
    Istanbul, Turkey
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Radio button problem in AJAX

    Hi,
    I have this ajax script
    ajax.js
    Code:
    var http_request = false;
    function update(formid,divid){
    document.getElementById(divid).innerHTML="Please wait ..."
    var form    = document.getElementById(formid);
    var http_request = false;
          http_request = false;
          if (window.XMLHttpRequest) {
             http_request = new XMLHttpRequest();
             if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml; charset=iso-8859-9');
             }
          } else if (window.ActiveXObject) {
             try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
                try {
                   http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
             }
          }
          if (!http_request) {
             alert('Cannot create XML HTTP instance.');
             return false;
          }
          http_request.onreadystatechange = alertContents;
          http_request.open('POST', "color.php", true);
          http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          http_request.setRequestHeader("Connection", "close");
          http_request.send(createQuery(form));
       function alertContents() {
          if (http_request.readyState == 4) {
          if (http_request.status == 200) {
           document.getElementById(divid).innerHTML=http_request.responseText;
               }else { alert('An error occurred! Please try again.');}
            } 
          }
    }
    function createQuery(form)
    {
        var elements = form.elements;
        var pairs = new Array();
        for (var i = 0; i < elements.length; i++) {
            if ((name = elements[i].name) && (value = elements[i].value))
                pairs.push(name + "=" + encodeURIComponent(value));
        }
        //pairs.push("param1=1");
        return pairs.join("&");
    }
    And, i'm using this form.
    form.php
    Code:
    <form style="margin: 0px;" name="form1" id="form1" method="post" action="">
    <p><input type="radio" name="color_id" value="1"></p>
    <p><input type="radio" name="color_id" value="3"></p>
    <p><input type="radio" name="color_id" value="5"></p>
    <p><input type="radio" name="color_id" value="7"></p>
    <input type="button" value=" Ok " onClick="javascript:update('form1','SeCiLeNReNK');">
    </form>
    And this code shown selected color.
    color.php
    Code:
    <?php
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
    ?>
    But, result always '7' ... I'm checked 1, 3 or 5 .. But result always 7 ...
    Please help me.
    Thanks.
    Eggdrop Inc. - Application Developer
    http://www.eggdrop.gen.tr/

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,512
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Didn't test it out myself, but try this:

    <input type="radio" name="color_id" id="color_id" value="1">

    I mention doing that because you have: getElementById

    It's looking for an ID, not a NAME.


  •  

    Posting Permissions

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