candies
candies

Reputation: 131

I have got wrong alert! What is wrong?

I have a problem in revealing the actual results of the radio buttons. I use javascript to get value from radio button, succeeded anyway, BUT when I create a condition where if value! = "Yes" alert that appears why still "OK", I would like to choose if it is "No" means alert that should be appear is "NOT OKE ".

This is the form:

<form id="form_test" name="form_test" method="POST">
<input type="hidden" id="menu_id" name="menu_id" value="<?=$menu_id?>"> 
<table align='center' width='auto' class='table1' cellpadding='5' cellspacing="0"> 
<tr> 
<td align='center' class='tabletitle'>No</td> 
<td align='center' class='tabletitle'>PC</td> 
<td align='center' class='tabletitle'>PN</td> 
<td align='center' class='tabletitle'>PCO</td> 
<td align='center' class='tabletitle'>GC</td> 
<td align='center' class='tabletitle'>CUR</td> 
<td align='center' class='tabletitle'>Codes</td>"; 
<td align='center' class='tabletitle'>Percent</td>"; 
<td align='center' width='5%' class='tabletitle'>Yes</td>"; 
<td align='center' width='5%' class='tabletitle'>No</td>"; 
</tr> 

[call to database] 

$num++; 

echo " <tr> 
<td class='tablecontent' align='center' valign='top'>$num</td> 
<td class='tablecontent' valign='top'>$pc</td> 
<td class='tablecontent' valign='top' nowrap>$pn</td> 
<td class='tablecontent' align='center' valign='top'>$pco</td> 
<td align='center' class='tablecontent' align='center' valign='top'>$gc</td> 
<td class='tablecontent' align='center' valign='top'>$cur</td> 
<td class='tablecontent2' valign='top'>$codes</td>"; 
<td align='center' class='tablecontent' valign='top'>$percent</td>"; 
<td align='center' class='tablecontent' valign='top'><input type='radio' id='yes' name='cek$num' value='yes' checked></td>"; 
<td align='center' class='tablecontent' valign='top'><input type='radio' id='no' name='cek$num' value='no' ></td>"; 
</tr>"; 

echo " 
<tr> 
<td><input type='button' name='accept' value='accept' onclick='getaccept()'></td>"; 
</tr>"; 
echo "</table>"; 
echo "<input type='hidden' id='jmlcek' name='jmlcek' value='$num'>"; 
echo "</form>"; 

Then this is the Javascript codes to get value:

<script language="Javascript"> 
function getaccept(){ 
//var str = ''; 
var elem = document.getElementById('form_test').elements; 
var c = confirm("Yakin mau nerima?"); 
for (var i = 1; i <= elem.length; i++) 
{ 
var e = elem[i].value 
if (elem[i].type == "radio" && c) 
{ 
if (e != "yes" && elem[i].checked){ 
alert('NOT OKE'); 
return false; 
}else{ 
alert('OKE'); 
return true; 
} 
} 
} 
} 
</script> 

For example the amount of data that actually are three and all got selected "yes", when I select "no" at one of the existing number it will display alert "NOT OKE", but in fact when I select "no" the value I got was "yes" and alert up "OKE".

I have tried many times to make them correct but still the results are wrong, so what's wrong?

Thanks.

Upvotes: 0

Views: 106

Answers (2)

candies
candies

Reputation: 131

This is what I got:

function getApprove(){
    //var elem = document.getElementById('form_test').elements;
    var RadioButton = document.getElementsByTagName('input'); 
    var Approve = confirm("Are you sure?");

for (var i = 0; i < RadioButton.length; i++) 
    { 
        if (RadioButton[i].type == "radio" && Approve) 
        { 
            if(RadioButton[i].checked && RadioButton[i].value != "yes"){
                alert("NOT OKE"); 
                return false;
            }
        }
    }

for (var i = 0; i < RadioButton.length; i++) 
{ 
    if (RadioButton[i].type == "radio" && Approve) 
    { 
        if(RadioButton[i].checked && RadioButton[i].value == "yes"){
            alert("OKE"); 
            return true;
        }
    }
}

=>SOLVED<=

Thankyou so much p.campbell, for you concern and helping me m(_ _)m :)

Upvotes: 1

p.campbell
p.campbell

Reputation: 100587

What's happening here is that you're only evaluating the FIRST radio button because you've got a return statement in both those conditions in your (e != "yes" && elem[i].checked). It's exiting the function.

Remove your return statements near your alerts. They're exiting the function, and disallowing the loop to iterate through the form elements.

Here's a demo on JsBin.

Upvotes: 0

Related Questions