0

i'm trying to follow this tutorial guide on this website http://homepage.ntlworld.com/kayseycarvey/jss3p11.html?

However, i met some difficultly when doing so. Even though i did the exact same thing. Please guide me if you know so. Here are what i did:

<html>
    <script>
        function GetSelectedItem() {

            chosen = ""
            len = document.f1.r1.length

            for (i = 0; i <len; i++) {
                if (document.f1.r1[i].checked) {
                    chosen = document.f1.r1[i].value
                }
            }

            if (chosen == "") {
                alert("No Location Chosen")
            }
            else {
                alert(chosen) 
            }
        }
    </script>

    <body>
        <form name="f1">
        <Input type = radio Name = r1 Value = "NE">North East
        <Input type = radio Name = r1 Value = "NW">North West
        <Input type = radio Name = r1 Value = "SE">South East
        <Input type = radio Name = r1 Value = "SW">South West
        <Input type = radio Name = r1 Value = "midlands">Midlands
        </form>


    </body></script>
</html>

On a side note, do i have to do anything to the form in order to trigger the function GetSelectedItem ? Thanks in advance !

Eddy
  • 49
  • 1
  • 2
  • 7

4 Answers4

0

Add onsubmit="javascript:GetSelectedItem();" to your form tag.

Alex
  • 1,605
  • 11
  • 14
0

add GetSelectedItem ()function on onchange event of your radio button

     <input type="radio" onclick="GetSelectedItem()" value="NE" name="r1">
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
  • Wow this is exactly what i want. Thanks alot bro. On the other hand, do you know of any ways to store the selected value into local database instead of an alert? – Eddy Sep 06 '13 at 07:02
  • This will alert on the radio's click... And will make `alert("No Location Chosen")` unlogical – Itay Sep 06 '13 at 07:03
  • @Eddy by local database you mean sql database or something like array ????? – rajesh kakawat Sep 06 '13 at 07:04
  • Hmm, what i wanted to do was, when the user click on the radio button, the value of the radio button should be stored into a local database. So i was wondering if there's any way to switch from 'showing an alert' to 'Storing it to their local database'? – Eddy Sep 06 '13 at 07:07
  • @Eddy store it in javascript array – rajesh kakawat Sep 06 '13 at 07:08
  • @rajeshkakawat Local database as in, because in the first place, that particular application i doing needs an offline function. Meaning to say, when the user lost connection halfway when doing their quiz, those answers(radio box) that they have selected can be save automatically into their local database(Phone). So can i store it in javascript array if i want it to work this way? Thanks alot. – Eddy Sep 06 '13 at 07:17
  • @Eddy Use cookie that should help out – rajesh kakawat Sep 06 '13 at 07:23
0
window.addEventListener('load', function () {
for (var i = 0; i < document.f1.r1.length; i++) {
    document.f1.r1[i].addEventListener("click", GetSelectedItem);
}
}, false);

function GetSelectedItem() {
alert(this.value);
}

http://jsfiddle.net/udSbL/2/

0

try this

<input type="radio" onclick="autosubmit(this.value)" value="NE" name="r1">
<script type="text/javascript">
function autosubmit(value) {
window.location='update.php?radiovalue='+value;
}
</script>

update.php

$rbtn=$_GET['radiovalue'];

//here your update query 
chirag ode
  • 950
  • 7
  • 15