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 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Ajax and Radio Buttons Problem!

    I've looked this up online and since I'm not great with neither AJAX or JS I'm a little bit at a loss. I read that I needed to use ID tags, but that still didn't get me far. Can anyone take a look at this and help me figure out why the radio buttons don't send to the php script?

    Thanks in advance!!

    Code:
    <?
    $query = "SELECT * FROM spicegirls_tour ORDER BY 'id' ASC";
    $result=mysql_query($query);
    while($row = mysql_fetch_array($result)){ // loop through the result of the page.
    ?>
    <tr>
    <td><input type="radio" name="local" id="local[<? echo $row[id]; ?>]" value="<? echo $row[id]; ?>"></td>
    <td><? echo $row[local]; ?></td>
    </tr>
    <?
    }
    ?>
    Code:
    	<script language="Javascript">
    
    //  You don't need to change anything in this function:
    
    function xmlhttpPost(strURL) {
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        self.xmlHttpReq.open('POST', strURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
            if (self.xmlHttpReq.readyState == 4) {
                updatepage(self.xmlHttpReq.responseText);
            }
        }
        self.xmlHttpReq.send(getquerystring());
    }
    
    //   The following function obtains two variables from your form (email and message) 
    //   and builds a string that gets sent to your PHP script.  Change this function to
    //   obtain whatever fields you want from your form.
    
    function getquerystring() {
        var form     = document.forms['form1'];
        var local = form.local.value;
        var username = form.username.value;
        qstr = 'local=' + escape(local) + '&username=' + escape(username); 
        return qstr;
    }
    
    function updatepage(str){
        document.getElementById("result").innerHTML = str;
    }
    </script>

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    You have multiple radio buttons all named "local", your user clicks on one radio button and it becomes checked.
    The line "var local = form.local.value;" simply doesn't make sense as form.local will reference a collection of inputs (which won't have a value property).

    You need to loop round the collection of radio buttons, find out which one's been checked and then use the value from the checked radio button.

    Replace "var local = form.local.value;" with the following;
    Code:
    var local;
    for (var i = 0; i < form.local.length; i++) {   
       if(form.local[i].checked){
          local = form.local[i].value;
       }
    }

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    It's good to have the break statement once the checked radio is found in order to keep the loop from doing unnecessary iteration.

    Code:
    var local;
    for (var i = 0; i < form.local.length; i++) {   
       if(form.local[i].checked){
          local = form.local[i].value;
          break;
       }
    }

  • #4
    New Coder
    Join Date
    Sep 2004
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks so much guys! This worked perfectly!!


  •  

    Posting Permissions

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