Hello All,
I am trying to validate multiple checkboxes whose values are stored in an array using php. I have been trying from a really long time but nothing's working out.
Can anyone help?
Here's the code: -
<form id="create_user" name="create_user" method="post" action="creating_database.php" onsubmit="return validate_create_user_form();">
-
<?php
-
$priviliges = array("SELECT","INSERT","UPDATE","DELETE","FILE","CREATE","ALTER","INDEX","DROP","CREATE TEMPORARY TABLES","CREATE VIEW","SHOW VIEW","CREATE ROUTINE","ALTER ROUTINE","EXECUTE","GRANT","SUPER","PROCESS",
-
"RELOAD","SHUTDOWN","SHOW DATABASES","LOCK TABLES", "REFERENCES", "REPLICATION CLIENT", "REPLICATION SLAVE");
-
-
echo "<table border=0 cellpadding=0 cellspacing=0 align=center width='100%' border='1'>";
-
$count = count($priviliges)."<br>";
-
//echo $count;
-
-
for($i = 0; $i < $count; $i++ )
-
{
-
if($i == 0)
-
echo "<tr>";
-
-
echo "<td align=left><input type=checkbox name=priviliges[]
-
value='$priviliges[$i]'>$priviliges[$i]</td>";
-
echo "</tr>";
-
}
-
-
echo "</table>";
-
?>
-
</form>
-
I would want to validate the priviliges[] array which consists of the above mentioned values. If no privilige is checked, i want to display an error message saying to check atleast one privilige.
Any help would be appreciated.
Thanks and Regards,
Dream2rule
6 15608
Use - document.getElementsByName("priviliges[]");
to get all the checkboxes in an array. Then loop over the checkboxes and keep checking the checked property. If none are checked, alert the error message. I assume you would do this onsubmit.
PS. privilege is spelled incorrectly!
Tried doing it this way but it does not help. :( -
-
for ( var i=0; i < document.getElementsByName("priviliges[]"); i++)
-
//priviliges is the name of the group of checkboxes
-
{
-
if (document.getElementsByName("priviliges[i]").checked)
-
{
-
var priviliges_checked[i] = document.getElementsByName("priviliges[i]").value;
-
}
-
}
-
if( (priviliges_checked[i] == null) || (priviliges_checked[i] == "") )
-
{
-
alert("Please check atleast 1 privilege!");
-
return false;
-
}
-
-
- var privs = document.getElementsByName("priviliges[]");
will give you an array. To get the count, use:
and to index an item in the array:
so your code could be: - var privs = document.getElementsByName("priviliges[]");
-
var privschecked = false;
-
for (var i = 0; i < privs.length; i++) {
-
if (privs[i].checked) {
-
privschecked = true;
-
break;
-
}
-
}
-
if (!privschecked) {
-
alert('blah');
-
return false;
-
}
-
return true;
thanks a lot.. it helped :)
Hi,
your code is very useful for me .thanks alot...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: varois83 |
last post by:
Hi
Newbie here. I have been working on creating a guestbook for my site as
practice and am learning a lot.
Do you guys validate your forms first on the client with javascript and
then on the...
|
by: Ralph Freshour |
last post by:
I have multiple checkbox's created with an array name because I have
many on the same web page - their names are like:
frm_chk_delete
frm_chk_delete
frm_chk_delete
frm_chk_delete
etc.
Here...
|
by: guitarsajee |
last post by:
hi..
i want to know how to validate file tag using javascript...
plz let me know soon as possible.. thanx...
|
by: menmysql |
last post by:
hi to all
i want to send multiple parameters using javascript. i am trying as follows
<a href="./Admin.jsp?per=null?per1=null">
|
by: mishasoft |
last post by:
Hi all,
I have a HTML multiple select (ListBox) control:
<select id="mySelect" multiple>
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
...
|
by: ghjk |
last post by:
I have a web site with several web pages. They are having different number of variables. eg: first page having 4 variables in the form(Name, Phone, ID, Gender)
second page having 10 variables in the...
|
by: BabyLucifer666 |
last post by:
Hello All, (using Access2000)
I have a form with multiple unbound checkboxes. What I would like to do is have the user check whoever needs to take a specific training course. My database is...
|
by: moose hoss |
last post by:
How would I validate this select box using JavaScript?
<form action="pizza/data.php" method="post" name="form"
onsubmit="javascript:return form_validation();"/>
<select name="team"...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |