| | |
Javascript checkbox validation
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 511
Reputation:
Solved Threads: 0
Hi,
How do i check if any of these checkboxs are selected. If nothing is selected then invalidate the form. Javascript
thanks
How do i check if any of these checkboxs are selected. If nothing is selected then invalidate the form. Javascript
thanks
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<input type="checkbox" name="MYcheckbox[]" value="0"> <input type="checkbox" name="MYcheckbox[]" value="1"> <input type="checkbox" name="MYcheckbox[]" value="2"> <input type="checkbox" name="MYcheckbox[]" value="3">
You can iterate trough forum controls with Javascript
javascript Syntax (Toggle Plain Text)
var i; for(i=1;i<18;i++) { document.formPermissions.elements[i].checked = 1; }
What you need to do in PHP is give each checkbox an individual ID then,
as in:
Now you can iterate trough them in javascript by doing this:
Hope this helps ^^
brecht
as in:
php Syntax (Toggle Plain Text)
echo "<input type='checkbox' id='checkbox".$i."' name='".$checkboxName."'> </input>";
Now you can iterate trough them in javascript by doing this:
javascript Syntax (Toggle Plain Text)
var i; var isItChecked; for(i=0;i<9;i++) { isItChecked = document.myFormName.elements[i].checked; }
Hope this helps ^^
brecht
I should stop writing my posts so late, anyway
Looking back at my previous post I don't even think you HAVE to give them a unique ID to be able to iterate trough them. ID is there to give each element a unique identifier though, I cannot locate that site you talk of, maybe you can point me to it so I can read up on that. Anyway, if you could give me a piece of code I could try and help with that, see where that goes.
Looking back at my previous post I don't even think you HAVE to give them a unique ID to be able to iterate trough them. ID is there to give each element a unique identifier though, I cannot locate that site you talk of, maybe you can point me to it so I can read up on that. Anyway, if you could give me a piece of code I could try and help with that, see where that goes.
•
•
Join Date: Apr 2008
Posts: 511
Reputation:
Solved Threads: 0
I want to add a javascript to check if any checkbox is selected or not.
Thanks
Thanks
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <title>Untitled Document</title> </head> <body> <form name="form1" method="post" action="save.php"> <table> <tr> <td>ID</td> <td>COUNTRY</td> <td>Unit</td> <td> </td> </tr> <?php $connection=mysql_connect("localhost", "root", ""); if ($connection) { mysql_select_db("phptest", $connection); } else { echo "Database connection was failed"; exit(); } $sql="SELECT id, name FROM country_1"; $runSql=mysql_query($sql); if (@mysql_num_rows($runSql)>0) { while ($newArray=mysql_fetch_array($runSql)) { ?> <tr> <td><?php echo $newArray['id']; ?></td> <td><?php echo $newArray['name']; ?></td> <td><input type="textbox" name="textboxUnit[<?php echo $newArray['id']; ?>]" value=""></td> <td><input type="checkbox" name="checkboxCountry[<?php echo $newArray['id']; ?>]" value="<?php echo $newArray['id']; ?>"></td> </tr> <?php } } ?> </table> </form> </body> </html>
Last edited by veledrom; Dec 8th, 2008 at 1:20 pm. Reason: no code
javascript Syntax (Toggle Plain Text)
onclick="if(this.checked == 1) { alert('checked: '+<? echo $newArray['id'];?>); }"
Hopefully this is what you want : - )
Last edited by brechtjah; Dec 8th, 2008 at 5:01 pm.
![]() |
Similar Threads
- JavaScript form validation - please help (JavaScript / DHTML / AJAX)
- Form Validation Issues (JavaScript / DHTML / AJAX)
- need help requiring at least one checkbox from group to be selected (JavaScript / DHTML / AJAX)
- Why javascript does not work on firefox? (JavaScript / DHTML / AJAX)
- Accessing HTML checkboxes with php and javascript (PHP)
- Simple checkbox validation question (JavaScript / DHTML / AJAX)
- how to validate checkbox with different names? (HTML and CSS)
- how to retrieve textfield for more than 1 checked records? (ASP)
- Javascript, Form fields validation and submit (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: print selected text in a html page
- Next Thread: JavaScript/PHP Sessions
Views: 4962 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxhelp animate array automatically autoplay beta boarder box bug button calendar captcha card cart codes column cookies createrange() css cursor date debugger decimal design developer dom download dropdown element enter error events firefox firehose flash focus form frameworks getselection google gwt html htmlform iframe image() index java javascript javascripts jawascriptruntimeerror jquery jsp listbox maps marquee masterpage menu microsoft mimic mp3 mp4 offline onmouseover parameters php player post problem programming progressbar prototype rating redirect regex safari scale scriptlets search select size sources sql starrating text textarea toggle twitter validation variables w3c web website window windowofwords windowsxp xml xspf





