SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2006
    0 Post(s)
    0 Thread(s)

    Radio buttons using AJAX always giving the same value

    I am using this code using AJAX. The probelm is no matter what choice is selected I always get the result grade=5. Can anyone help me with why this is the case? Thanks, Steven.

    <li><input type="radio" id="grade" name="grade" value="5" accesskey="5">Great</li>
    <li><input type="radio" id="grade" name="grade" value="4" accesskey="4">Good</li>
    <li><input type="radio" id="grade" name="grade" value="3" accesskey="3">OK</li>
    <li><input type="radio" id="grade" name="grade" value="2" accesskey="2">Poor</li>
    <li><input type="radio" id="grade" name="grade" value="1" accesskey="1">Oh, oh</li>
    <li><input type="radio" id="grade" name="grade" value="0" accesskey="0">What is this?</li>
    <li><input value="Study now"
    onClick="getNewTotals();" >


    Now that a grade is entered it should be picked up by this text:

    function createQueryString() {
    var grade= document.getElementById("grade").value;
    var queryString = "grade=" +grade;
    return queryString;


    function getNewTotals() {
    var url = "getUpdatedSales.php";
    url = url + "?dummy=" + new Date().getTime();
    var queryString= createQueryString();"POST", url, true);
    request.onreadystatechange = updatePage;
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


    Why do I always get a result of grade=5?
    Last edited by stevenodonnell; Aug 1, 2006 at 20:37.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2005
    0 Post(s)
    0 Thread(s)
    You should do this on each line to get the value:

    PHP Code:
    <li><input onClick="getNewTotals(this.value)" type="radio" id="grade" name="grade" value="5" accesskey="5">Great</li

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Sydney, NSW, Australia
    25 Post(s)
    1 Thread(s)
    You can't use the same id multiple times. Each id must be unique. To determine which radio button is selected check which of the entries in the graded array has selected set to true and then get the value from that entry in the array. See for more info on radio button validation and how to determine which one is selected.
    Stephen J Chapman, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Posting Permissions

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