views:

1415

answers:

2

Hello,

I'm going to get the checked radio button value using AJAX in PHP. I have done the following code, but i can't continue.

I need your help.

ajax_radio.php

<html>
  <head>
    <script type="text/javascript" src="ajax.js" ></script>
  </head>
  <body>
    <input type="radio" name="radio1">Blue</input>
    <input type="radio" name="radio2">White</input>
    <input type="radio" name="radio3">Red</input>
    <input type="button" name="btn" value="Click" onClick="hello('a')"></input><br />
   <input type="text" id="btn_get" name="get_btn_value"></input>
  </body>
</html>

ajax.js

var xmlHttp;
function GetXmlHttpObject(){
    try{
        xmlHttp = new XMLHttpRequest();
    }
    catch(e){
        try{
            xmlHttp = new ActiveXObject(Msxml2.XMLHTTP);
        }
        catch(e){
            try{
                xmlHttp = new ActiveXObject(Microsoft.XMLHTTP);
            }
            catch(e){
                alert("doesn't support AJAX");
                return false;
            }
        }
    }
}
function hello(a){
    GetXmlHttpObject();
    xmlHttp.open("GET","ajax_radio.html?",true);
    xmlHttp.onreadystatechange = response;
    xmlHttp.send(null);
}
function response(){
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
            var radio_text = xmlHttp.responseText;
            document.getElementById('btn_get').innerHTML = radio_text;
        }
    }
}

Do you know how to complete this? Thanks in advance. :)

Edit:

I can't post the code here right now, because of the low speed of network. I'm sorry.

I have shared it in rapidshare. Can you download it firstly, and help then? Thanks a lot.

I have to update it when i go back home. Too bad.

+1  A: 

because I mostly use Jquery I wrote here a jquery snipped that checks radio button values because it is much easier and cleaner:

<script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script>
    google.load("jquery", "1.4.0");
</script>
<script type="text/javascript">
    $(function() {
        $('#buttoncheck').click(function () {
            var var_name = $("input[name='radio1']:checked").val();
            $('#btn_get').val(var_name);
        });
    });
</script>
<html>
    <head>
        <script type="text/javascript" src="ajax.js" ></script>
    </head>
    <body>
        <input type="radio" value="Blue" name="radio1">Blue</input>
        <input type="radio" value="White" name="radio1">White</input>
        <input type="radio" value="Red" name="radio1">Red</input>
        <input id="buttoncheck" type="button" name="btn" value="Click"></input>
        <br />
        <input type="text" id="btn_get" name="get_btn_value"></input>
    </body>
</html>

Take a look for jquery here: Jquery

Explanation: This part selects the item with the ID #buttoncheck and checks if its ran: $('#buttoncheck').click(function () {

Then it will run the code in the function. This code gets the value of a input box with the name radio1. the ':checked' makes sure that it only selects the value of the checked radio button:

var var_name = $("input[name='radio1']:checked").val(); 

And last. This puts the value of the variable into the item with #btn_get as id:

$('#btn_get').val(var_name);
RJD22
@Thank you so much RJD22, you're so great!!! You've saved me and given me a good tutorial. Although it's done, i have to dive in it. I'm learning to use jQuery now, but it's still hard for me to understand your code. And i still want to try to realize it with pure ajax.
garcon1986
@garcon1986: jQuery is "pure" JS that uses "pure" Ajax. The only thing it is to make your life easier by not have to worry about cross browser compatibility and some other nice helpers.
Felix Kling
@Felix, Thanks for your explanation. jQuery is a pretty good tool for developpers. Cheers.
garcon1986
@garcon1986: The snipped I wrote is really simple. Because you want to learn how to use it I added a small explanation
RJD22
@RJD22, Thanks a lot, i'm trying to understand all the lines and use jQuery in my application. ;)
garcon1986
@thanks RJD22, I have one more question.That is Can i use the radio button value as a parameter in a mysql Query in the same page?
garcon1986
not directly from javascript. If you want to use it within php you will need to post it. take a look at:http://api.jquery.com/jQuery.post/
RJD22
A: 

hi all, Plz help me, how to insert one of any checkbox into database using ajax?

mckd