Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:40:13+00:00 2026-06-18T14:40:13+00:00

I’m writing a form whereby multiple checkboxes can be selected and saved into a

  • 0

I’m writing a form whereby multiple checkboxes can be selected and saved into a MySQL database. However, I now want to limit the number of checkboxes a user can select using javascript, and have run into some trouble.

It seems for the import function to still work, each checkbox needs to have the same name as each other so they can be identified and imploded, however for the Javascript function to work each name needs to be different so that they can be differentiated.

Any suggestions would be gratefully received!

Here’s the code that I’ve been playing around with.

Javascript function:

<?php $calc = mysql_query($query) or die(mysql_error()); ?>

<SCRIPT LANGUAGE="javascript">

function KeepCount() 
{
var NewCount = 0

<?php while($count_row = mysql_fetch_array($calc)){
echo "

if (document.import1.checkbox".$count_row['id'].".checked)
{NewCount = NewCount + 1}

";
} ?>

if (NewCount == 6)
{
alert('You can select up to 5 competitors. If you wish to import more you will need to add more judges.')
document.import1; return false;
}
} 
</SCRIPT>

Form:

<form action="import_process.php" method="post" name="import1" id="import1">
<input name="import" type="submit" id="import" value="Import">

<table>
<tr>
<td>&nbsp;</td>
<td>Name</td>
<td>Gender</td>
</tr>

<?php $counter = 1;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<tr bgcolor=white>";
echo "<td align=\"center\">
<input name=\"checkbox".$row['id']."\" type=\"checkbox\" id=\"checkbox[]\" value=\"".$row['id']."\" onClick=\"return KeepCount()\"></td>";
}
echo "<td>".$row['name']."</td>";
echo "<td>".$row['gender']."</td>";
echo "</tr>";
$counter++;
}
?>
</table>
</form>

import_process.php:

if(isset($_POST['checkbox'])){
$checkbox = $_POST['checkbox'];
$id = "('" . implode( "','", $checkbox ) . "');" ; 
$sql="
INSERT INTO..."
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:40:14+00:00Added an answer on June 18, 2026 at 2:40 pm

    It’s a little bit difficult to understand your code, but i think you get confused by the difference of name an id. Id must be different in every checkbox, name must be the same:

    <input id=\"checkbox".$row['id']."\" type=\"checkbox\" name=\"checkbox[]\" value=\"".$row['id']."\" onClick=\"return KeepCount()\"></td>";
    

    And the name-attribute is the one, wich needs the [], so you get the result in the array $_POST['checkbox'].

    The second is, that your function KeepCount seems to check always the same checkbox (or I really don’t understand how it works) because whatever $count_row['id'] is, it’s set once while the page is parsed so the javascript function cannot really count anything.

    So change the checkbox-creation like this:

    <input id=\"checkbox".$row['id']."\" type=\"checkbox\" name=\"checkbox[]\" value=\"".$row['id']."\" onClick=\"return KeepCount(this)\"></td>";
    

    and the javascript like this

    var count = 0;    
    
    function KeepCount(it) {
     if (it.checked) {
      count = count + 1;
     } else {
      count = count - 1;
     }
     if (count == 6) {
      alert('You can 
      etc.
    

    That should work.

    By the way: value=row[‘id’] seems to be nonsense too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a reasonable size flat file database of text documents mostly saved in
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.