views:

66

answers:

3

Hi I have a check box when check that check box the id corresponding to the check box is placed on a text box ... but when there is only single value in the database i cant get the check value why? here is my code

<? if(isset($AcceptFriend)) {?>
<form action="<?=site_url()?>friends/Accept_Friend" name="orderform" id="orderform" method="post" style="background:#CCCC99">
<input type="text" name="chId" id="chId" >
<table  border="0" height="50%" id="chkbox" width="50%" >
<tr>
<? foreach($AcceptFriend as $row)
{?>
<tr>
<td>Name</td><td><?=$row['dFrindName'].'</br>';?></td>
<td> <input type="checkbox" name="checkId" id="checkId" value="<? echo  $row['dMemberId']; ?>" onClick="get_check_value()" ></td>
</tr>

<? }}?>
</tr>
 <tr> <td width="10px"><input type="submit" name="submit" id="submit" class="buttn" value="AcceptFriend"></td></tr>

</table>


</form>

This is the script i am using

function get_check_value()
{
    var c_value = "";

    for (var i=0; i < document.orderform.checkId.length; i++)
       {
       if (document.orderform.checkId[i].checked)
          {
          c_value = c_value + document.orderform.checkId[i].value + "\n";
          }
       }
       alert(c_value);
       document.getElementById('chId').value= c_value;
}
A: 

You should give your checkbox an array-like name attribute: "checkId[]" And the ID attribute should be different for each checkbox, checkId14 or whatever you can track later.

andr
no it is not workin actually i am getting the values if there are more than one checkbox but if the length of the checkboxis 1 then it is not available
udaya
@udaya: The note is correct, so please make the change, but it also isn't exactly an answer, so -1.
Matchu
@udaya: well, i thought that would make it work, didn't test actually.Did you try that? It could be that if there's only one element with a name "checkId", there's no array and the value is in document.orderform.checkId.value
andr
A: 

Andr is right and You better not downvote a man for a healthy critique - His answer may not solved Your problem, but html ids should hold unique value, class identifiers can have all the same name.

arunas_t
Neither of these were answers, so they should be in comments. Welcome to StackOverflow, and please don't get upset with people for encouraging good behavior.
Matchu
Ha ha I just wanted stand for Andr who noticed something and was downvoted for that, thank You. I can just enjoy that there are many programmers that I can learn from and many people that I can help to, pity that You are not one of them.
arunas_t
@Matchu: i was actually puzzled by the fact that the original code worked at all ;)
andr
@arunas_t: Feedback like this is fantastic, and please keep it up! At the same time, please put feedback like that in the comments for the answer, rather than an individual answer, since the former doesn't make any organizational sense. It's fine - now you know :) "Answers" are supposed to answer the initial question, rather than consist of dialogue, like this does here.
Matchu
Ok, sorry, I'll try.
arunas_t
+3  A: 

If you use an ID once, the returning value of document.orderform.checkId.length will give you the number of properties of that object because document.orderform.checkId will be that single object.

In your loop you will walk thru all these properties and ask them on their property "checked". Because they don't have that property, you will get errors and your script will fail.

When using an ID more than once, document.orderform.checkId will become an array of all objects of that ID and length will return you the number of them.

In that case your loop will walk thru all the checkboxes as you have intended.

The best solution would be to use document.getElementsByName('checkId') or to add a hidden dummy checkbox which will be ignored by starting the loop with i=1 instead of i=0;

Since ID's should be unique, I would recommend to change your scripts to use the name attribute and use document.getElementsByName('checkId') which will always return an array.

favo