PDA

View Full Version : radiobutton value with id storing in a variable



lampie1978
01-03-2012, 10:02 PM
Hi all,

This probably has a simple solution, but i am not seeing it anymore.
I want the value from a radio button inserted in a variable, but all i get is "undefinied" as an answer.

My radiobuttons are in a form called 'personalia':


<input type="radio" name="geslacht" value="m" id="geslacht_m" onchange="personalia_showform();"/><label for="geslacht_m">Man</label>
<input type="radio" name="geslacht" value="v" id="geslacht_v" onchange="personalia_showform();"/><label for="geslacht_v">Vrouw</label>


Then when i hit the submit button, i am calling a javascript which should be entering the value into a variable.
My variable declaration:


var pers_gesl = document.personalia.geslacht.value;


Then when i show the pers_gesl on my screen it only says: undefinied.
What am i doing wrong?

Thanks in advance.

regicidedelferoz
01-04-2012, 03:08 AM
html



<input type="radio" name="geslacht" value="m" id="geslacht_m" onchange="personalia_showform(this);"/><label for="geslacht_m">Man</label>
<input type="radio" name="geslacht" value="v" id="geslacht_m" onchange="personalia_showform(this);"/><label for="geslacht_v">Vrouw</label>




javascript



<script type="text/javascript">
function personalia_showform(obj){
var pers_gesl = obj.value;
}
</script>




is this is not what you are looking for,. can you please post your whole code here?

lampie1978
01-04-2012, 10:50 AM
No that's not it, but thanks anyway.
I'll post all the necessary code down below. I am not going to post all code, cause that's more than 700 lines of HTML-code alone.

HTML


<form name="personalia" action="personalia.htm" method="post" enctype="text/plain">
<div class="antw"><input type="radio" name="geslacht" value="m" id="geslacht_m" onchange="personalia_showform();"/><label for="geslacht_m">Man</label></div>
<div class="antw"><input type="radio" name="geslacht" value="v" id="geslacht_v" onchange="personalia_showform();"/><label for="geslacht_v">Vrouw</label></div>
<div class="antw"><input type="radio" name="geslacht" value="st_mv" id="geslacht_mv" onchange="personalia_showform();"/><label for="geslacht_mv">Man/Vrouw-stel</label></div>
<div class="antw"><input type="radio" name="geslacht" value="st_vv" id="geslacht_vv" onchange="personalia_showform();"/> <label for="geslacht_vv">Vrouw/Vrouw-stel</label></div>
<div class="antw"><input type="radio" name="geslacht" value="st_mm" id="geslacht_mm" onchange="personalia_showform();"/><label for="geslacht_mm">Man/Man-stel</label></div>

<input type="submit" value="Bewaren" onclick="formulier_controle_personalia();" onkeypress="formulier_controle_personalia();">
</form>


The function personalia_showform() let's me show another set of questions that's related to the gender of the person, so that's not the code that needs to be altered.
The function formulier_controle_personalia() is the function that has the declaration of the variables. And the id from the radio button is necessary for the <label> part.

Javascript:


function formulier_controle_personalia()
{
var pers_gesl = document.personalia.geslacht.value;

alert("Personalia wordt gecontroleerd!\npers_gesl: " +pers_gesl)
}

regicidedelferoz
01-05-2012, 12:52 AM
try this one sir,.



function formulier_controle_personalia(){
var pers_gesl = document.personalia.geslacht.value;
i = document.getElementsByName('geslacht').length;
for(ctr=0; ctr<=i; ctr++){
if(document.personalia.geslacht[ctr].checked){
pers_gesl = document.personalia.geslacht[ctr].value;
alert("Personalia wordt gecontroleerd!\npers_gesl: " +pers_gesl)
}
}
}

lampie1978
01-05-2012, 02:45 PM
ok that works.. thanks...

but how do i implement this for more then 20 different variables that all come from radio or check-buttons on the same page??
and still throw in a other checks on those variables after they have been declared...
and i really don't like to nest 20 or more functions like these within each other?? do i need an array or something??

i know, i'm making it difficult on you..
but this way i am learning from it also..

regicidedelferoz
01-06-2012, 08:16 AM
is your radio buttons and check buttons have the same names?,.

if so, you have nothing to worry,. the code above can do it,.

lampie1978
01-06-2012, 10:14 AM
nope all groups off radio or checkbuttons have different names for example:



var pers_rel = document.personalia.relatie.value;
var pers_p1_post = document.personalia.p1_postuur.value;
var pers_p1_ogen = document.personalia.p1_ogen.value;
var pers_p2_post = document.personalia.p2_postuur.value;
var pers_p2_ogen = document.personalia.p2_ogen.value;


and many more where these came from..

regicidedelferoz
01-07-2012, 02:28 AM
try this one



function formulier_controle_personalia(){
radios = document.getElementsByTagName('input');
for (x=0; x<radios.length; x++) {
if(radios[x].checked){
rdoName = radios[x].name;
rdoValue = radios[x].value;

/* declaring variables */
if(rdoName==name_of_a_radio_group_name)
var variable_name = rdoValue
else if(rdoName==another name of radio group name) then
var another_variable = rdoValue
}
}
}


feel free to ask question.. hope this one is the code you are looking for,.

lampie1978
01-08-2012, 06:18 PM
Hi again,

Well it isn't working for so far. I really thought u had it this time, the code seems plausible.

I tried every variation on the code i possibly know, but nothing has worked so far..

Maybe it's better for me to post both files for u to try it yourselfs.
So u can find the .htm and .js in the ZIP-file in this post also.

Thanks again.

regicidedelferoz
01-09-2012, 05:18 AM
try this one,.



/************************************************************************/
/* */
/* formulier controles */
/* */
/************************************************************************/

function formulier_controle_personalia()
{
radios = document.getElementsByTagName('input');
for (x=0; x<radios.length; x++)
{
if(radios[x].checked)
{
rdoName = radios[x].name;
rdoValue = radios[x].value;

/* declaring variables */
if (rdoName=='geslacht') var pers_gesl = rdoValue
else if(rdoName=='relatie') var pers_rel = rdoValue
else if(rdoName=='p1_postuur') var pers_p1_post = rdoValue
else if(rdoName=='p1_ogen') var pers_p1_ogen = rdoValue
else if(rdoName=='p1_haar') var pers_p1_haar = rdoValue
else if(rdoName=='p1_huidskleur') var pers_p1_huid = rdoValue
else if(rdoName=='p1_roken') var pers_p1_roken = rdoValue
else if(rdoName=='p1_tatoeage') var pers_p1_tatoe = rdoValue
else if(rdoName=='p1_piercing') var pers_p1_piercing = rdoValue
else if(rdoName=='p1_penis') var pers_p1_penis = rdoValue
else if(rdoName=='p1_geaardheid') var pers_p1_geaardheid = rdoValue
else if(rdoName=='p1_borsten') var pers_p1_borsten = rdoValue
else if(rdoName=='p1_geschoren') var pers_p1_scheren = rdoValue
else if(rdoName=='p2_postuur') var pers_p2_post = rdoValue
else if(rdoName=='p2_ogen') var pers_p2_ogen = rdoValue
else if(rdoName=='p2_haar') var pers_p2_haar = rdoValue
else if(rdoName=='p2_huidskleur') var pers_p2_huid = rdoValue
else if(rdoName=='p2_roken') var pers_p2_roken = rdoValue
else if(rdoName=='p2_tatoeage') var pers_p2_tatoe = rdoValue
else if(rdoName=='p2_piercing') var pers_p2_piercing = rdoValue
else if(rdoName=='p2_penis') var pers_p2_penis = rdoValue
else if(rdoName=='p2_borsten') var pers_p2_borsten = rdoValue
else if(rdoName=='p2_geaardheid') var pers_p2_geaardheid = rdoValue
else if(rdoName=='p2_geschoren') var pers_p2_scheren = rdoValue

var pers_nick = document.personalia.nickname.value;
var pers_email = document.personalia.email.value;
var pers_ww = document.personalia.wachtwoord.value;
var pers_wwchk = document.personalia.wachtwoord_check.value;
var pers_prov = document.personalia.provincie.value;
var pers_p1_gebdat = document.personalia.p1_dag.value +"/"+ document.personalia.p1_maand.value +"/"+ document.personalia.p1_jaar.value;
var pers_p1_lengte = document.personalia.p1_lengte.value;
var pers_p2_gebdat = document.personalia.p2_dag.value +"/"+ document.personalia.p2_maand.value +"/"+ document.personalia.p2_jaar.value;
var pers_p2_lengte = document.personalia.p2_lengte.value;
}
}

alert("Personalia wordt gecontroleerd!\npers_nick: " +pers_nick+ "\npers_email: " +pers_email+ "\npers_ww: " +pers_ww+ "\npers_wwchk: " +pers_ww
+ "\npers_prov: " +pers_prov+ "\npers_gesl: " +pers_gesl+ "\npers_rel: " +pers_rel+ "\npers_p1_gebdat: " +pers_p1_gebdat
+ "\npers_p1_lengte: " +pers_p1_lengte+ "\npers_p1_post: " +pers_p1_post+ "\npers_p1_ogen: " +pers_p1_ogen+ "\npers_p1_haar: " +pers_p1_haar
+ "\npers_p1_huid: " +pers_p1_huid+ "\npers_p1_roken: " +pers_p1_roken+ "\npers_p1_tatoe: " +pers_p1_tatoe+ "\npers_p1_piercing: " +pers_p1_piercing
+ "\npers_p1_penis: " +pers_p1_penis+ "\npers_p1_borsten: " +pers_p1_borsten+ "\npers_p1_geaardheid: " +pers_p1_geaardheid
+ "\npers_p1_scheren: " +pers_p1_scheren+ "\npers_p2_gebdat: " +pers_p2_gebdat+ "\npers_p2_lengte: " +pers_p2_lengte+ "\npers_p2_post: " +pers_p2_post
+ "\npers_p2_ogen: " +pers_p2_ogen+ "\npers_p2_haar: " +pers_p2_haar+ "\npers_p2_huid: " +pers_p2_huid+ "\npers_p2_roken: " +pers_p2_roken
+ "\npers_p2_tatoe: " +pers_p2_tatoe+ "\npers_p2_piercing: " +pers_p2_piercing+ "\npers_p2_penis: " +pers_p2_penis
+ "\npers_p2_borsten: " +pers_p2_borsten+ "\npers_p2_geaardheid: " +pers_p2_geaardheid+ "\npers_p2_scheren: " +pers_p2_scheren);
}

lampie1978
01-10-2012, 07:49 AM
Thanks sir,

That did the trick. It's working fine.

And what if i want to do the same thing with checkboxes, but then a variable can have more then 1 answer from a checkboxgroup?