Jump to content

form with checkboxes


Pavlos1316

Recommended Posts

Hi to all (quite new still)

 

I have a form with checkboxes. It works fine.

It has 24 columns. 8 rows X 3 colums each.

 

I need when the form is submited to check if:

 

1. every row has at least 1 column checked

 

2. there are no more than 9 columns checked.

 

That means one column checked in every row exept one that will have 2 columns checked.

 

Can anyone help me? 

 

Thanks

 

P.S. cause until now I use only php where do I place javascript code? (begining, end?)

Link to comment
https://forums.phpfreaks.com/topic/110221-form-with-checkboxes/
Share on other sites

<html>
<head>
<script language="Javascript" type="text/javascript">

function checkForm(formObj){

  var double = false;

  for (var row=1; row<=8; row++) {

    var rowCount = 0;

    for (var col=1; col<=3; col++) {
      if (formObj['chk-'+row+'-'+col].checked) {
        rowCount++;
      }
    }

    if (rowCount==0) {
      alert('You must select at least one checkbox from each row');
      return false;
    }

    if (rowCount>2) {
      alert('You may not select more than two checkboxes from any row');
      return false;
    }

    if (rowCount==2) {

      if (double) {
        alert('You may not select two checkboxes in more than one row');
        return false;
      } else {
        double = true;
      }
    }

  }

  if (!double) {
        alert('You must select two checkboxes in one of the rows');
        return false;
  }

  return true;

}
</script>

</head>

<body>

<form name="theform" onsubmit="return checkForm(this);">
<table border="1">
  <tr>
    <td> </td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td><input type="checkbox" name="chk-1-1"></td>
    <td><input type="checkbox" name="chk-1-2"></td>
    <td><input type="checkbox" name="chk-1-3"></td>
  </tr>
  <tr>
    <td>2</td>
    <td><input type="checkbox" name="chk-2-1"></td>
    <td><input type="checkbox" name="chk-2-2"></td>
    <td><input type="checkbox" name="chk-2-3"></td>
  </tr>
  <tr>
    <td>3</td>
    <td><input type="checkbox" name="chk-3-1"></td>
    <td><input type="checkbox" name="chk-3-2"></td>
    <td><input type="checkbox" name="chk-3-3"></td>
  </tr>
  <tr>
    <td>4</td>
    <td><input type="checkbox" name="chk-4-1"></td>
    <td><input type="checkbox" name="chk-4-2"></td>
    <td><input type="checkbox" name="chk-4-3"></td>
  </tr>
  <tr>
    <td>5</td>
    <td><input type="checkbox" name="chk-5-1"></td>
    <td><input type="checkbox" name="chk-5-2"></td>
    <td><input type="checkbox" name="chk-5-3"></td>
  </tr>
  <tr>
    <td>6</td>
    <td><input type="checkbox" name="chk-6-1"></td>
    <td><input type="checkbox" name="chk-6-2"></td>
    <td><input type="checkbox" name="chk-6-3"></td>
  </tr>
  <tr>
    <td>7</td>
    <td><input type="checkbox" name="chk-7-1"></td>
    <td><input type="checkbox" name="chk-7-2"></td>
    <td><input type="checkbox" name="chk-7-3"></td>
  </tr>
  <tr>
    <td>8</td>
    <td><input type="checkbox" name="chk-8-1"></td>
    <td><input type="checkbox" name="chk-8-2"></td>
    <td><input type="checkbox" name="chk-8-3"></td>
  </tr>
</table>
<button type="submit">Submit</button>
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/110221-form-with-checkboxes/#findComment-566074
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.