/    Sign up×
Community /Pin to ProfileBookmark

Make sure checkbox is checked, then submit

while (…….)
<input type=”checkbox” name=”del[]” value='<?php echo $IPAddress;?>’><?php echo $IPAddress;?><p>
}

I have this loop to create the number of check boxes based on number of records from db.

What I would like is this: If no checkboxes are sellected, prompt
else if there is then only submit.

How can I do this? I am new at javascripts, hope someone can help

Thanks
James

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJan 27.2004 — Hi!

First of all: please do not use the '[]' in the names of your checkboxes!!

Another thing: you could give them unique names in your loop, which would make it easier to identify them in the script receiving the form data.

As far as the rest is concerned, here's some code:

The code from the top until the closing head tag:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var checked;
function checkForm(){
checked=0;
for (var i = 0; i < document.forms[0].elements.length; i++){
if (document.forms[0].elements[i].type=="checkbox"){
if (document.forms[0].elements[i].checked==true){
checked++;
}
}
}
if (checked==0){
alert('Please check at least one checkbox!');
return false;
}
else{
return true
}
}
//-->
</script>
</head>
[/code]

The rest:
[code=php]
<body>
<form action="myAction.php" name="myForm" method="post" onSubmit="return checkForm()">

<!-- begin while stuff... -->
<input type="checkbox" name="del" value='<?php echo $IPAddress;?>'><?php echo $IPAddress;?><p>
<!-- end while stuff... -->

<input type="submit" value="blah">
</form>
</body>
</html>
[/code]

Cheers - Pit
Copy linkTweet thisAlerts:
@james_cwyauthorJan 28.2004 — Thanks for the hellp.Really appreciate it.
Copy linkTweet thisAlerts:
@crh3675Jan 28.2004 — [B]Pittman:[/B]

[i]Originally posted by Pittimann [/i]

[B]Hi!



First of all: please do not use the '[]' in the names of your checkboxes!!

[/QUOTE]




What if the user is passing an array of elements? It is the only way for me to get the data for <select multiple> boxes. I sometimes need to use the PHP constructor "[]" to pass the values as an array.
Copy linkTweet thisAlerts:
@PittimannJan 28.2004 — Hi!

You're welcome, james_cwy!

To crh3675: if you use [] in the names of form elements and try to reference these elements by name in js, you will get errors (document.blah...[] is null or not an object) and the script wont work.

Originally posted by crh3675:What if the user is passing an array of elements? It is the only way for me to get the data for <select multiple> boxes. I sometimes need to use the PHP constructor "[]" to pass the values as an array.[/QUOTE]It is not the only way and it is not necessary to use the PHP constructor "[]" to pass the values as an array.

You can easily deal with that in a different way as far as your PHP is concerned, but - talking about js - the [] will "kill" your script if you do a reference by names...

Cheers - Pit
×

Success!

Help @james_cwy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...