Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX radio button problem 2

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
Here is my problem. After the user fills in the form objects, it is entered into the database via an AJAX event and not a form submission.
I simply pass the value of each form object by id to a page that inserts the data

Because I pass the values by getElementId I am having trouble with the radio buttons (who must have unique ID's). I am very new to AJAX and Javascript and need help figuring out a solution.

Here is my code.

HTML CODE

<td height='26' colspan='4'>Is the veteran deceased? Yes
<label>
<input type='radio' name='vet_deceased' id='vet_deceased1' value='1' />
No
<input type='radio' name='vet_deceased' id='vet_deceased2' value='2' />
</label></td>

AJAX CODE

if(document.getElementById('vet_deceased1').checked=true)
{
var vd = 1;
}
else if (document.getElementById 'vet_deceased2').checked=true)
{
var vd = 2;
}

(this is sent to a PHP page where vd is entered into the database)

Can someone help me figure out how to handle whether a radio button is checked or not and get the value of the checked radio button into a variable please.
 
One major issue you need to resolve is the usage of single (=) versus double (==) equals signs.

A single equals assigns a value, and a double equals tests a value.

In your code, you are using a single equals in a test:

Code:
if(document.getElementById('vet_deceased1').checked [!]=[/!] true)

This does not do as you expect (test the value), but rather sets the checked property to true.

Try fixing these logic errors and see how you go.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
In addition to Dan's suggestion:
If you have more than 2 radios it may get clumsy to use many if else statements.
You may want to to use a loop instead.

Just use getElementsByName with the radio buttons name and loop through them until you find the checked one.

Code:
var radios=document.getElementsByName('vet_deceased');
for(var i=0;i<=radios.length;i++){
if(radios[i].checked){
alert(radios[i].value);
}
[code]

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown. 

Behind the Web, Tips and Tricks for Web Development. 
[URL unfurl="true"]http://behindtheweb.blogspot.com/[/URL]
 
Thanks you guys, great advice and Vacunita's loop is working perfectly.

I tried to use the same loop for checkboxes and I get an 'undefined' error.

Here is a sample of the html form.

<tr>
<td><input type='checkbox' name='medical_diagnoses' id='Cataracts' value='Cataracts'>
Cataracts</td>
<td><input type='checkbox' name='medical_diagnoses' id='Emphysema' value='Emphysema' >
Emphysema</td>
<td><input type='checkbox' name='medical_diagnoses' id='History of Heart Attack' value='History of Heart Attack'>
History of Heart Attack</td>
<td><input type='checkbox' name='medical_diagnoses' id='Osteoporosis' value='Osteoporosis'>
Osteoporosis</td>
</tr>

here is the AJAX (javascript)

for (i=0;i<document.form1.medical_diagnoses.length;i++)
{

if (document.form1.medical_diagnoses.checked)
{
var md = md + document.form1.medical_diagnoses.value + "-";
}
}

here is the results. They will echo on the screen but will not insert into the database.The 'undefined' at the begining and the 'object' at the end seems to be the problem. I'm not sure where they are coming from.

undefinedCataracts-Emphysema-[object]

Any thoughts?
 
You are not subtracting 1 from the length. I missed it on the radios which should also be issuing an error.

Code:
for (i=0;i<document.form1.medical_diagnoses.length[red]-1[/red];i++) 
...




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
This also worked, I'm really not sure why, but it did.

I declared the var outside the loop and set it to blank.


var md = "";
for (i=0;i<document.form1.medical_diagnoses.length;i++)
{

if (document.form1.medical_diagnoses.checked)
{
md = md + document.form1.medical_diagnoses.value + "-";
}
}

Anyways, it's working now. so thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top