mardi 18 mai 2021

swapping checkboxes in PHP using javascript

trying to flip-flop values of two checkbox arrays in PHP using javascript from a hyperlink. the issue i am facing is that php wants the checkbox array to use [ ] and javascript does not.

Here is my code...what code would you suggest to make the checked items in checkbox2 = checkbox1 AND checkbox1 = checkbox2?

<a id='swap' href=\"javascript: swapContacts();\">Swap</a>
<br>
<input class='c_contactmethods' type='checkbox' name='checkbox1[]' value='1'>Phone<br>
<input class='c_contactmethods' type='checkbox' name='checkbox1[]' value='2'>Email<br>
<input class='c_contactmethods' type='checkbox' name='checkbox1[]' value='3'>Text<br>
<input class='c_contactmethods' type='checkbox' name='checkbox1[]' value='4'>Mail<br>
<br>
<input class='c_co_contactmethods' type='checkbox' name='checkbox2[]' value='1'>Phone<br>
<input class='c_co_contactmethods' type='checkbox' name='checkbox2[]' value='2'>Email<br>
<input class='c_co_contactmethods' type='checkbox' name='checkbox2[]' value='3'>Text<br>
<input class='c_co_contactmethods' type='checkbox' name='checkbox2[]' value='4'>Mail<br>

I need help with the code for swapContacts() in javascript, please. Here is what I am trying to use:

var i=0;    
for (i=0;i<document.form1.checkbox1.length;i++){
    $tempstr = document.form1.checkbox1[i].value;
    document.form1.checkbox1[i].value= document.form1.checkbox2[i].value;
    document.form1.checkbox2[i].value= $tempstr;
}



Aucun commentaire:

Enregistrer un commentaire