Jump to content

Here's a tought one...!!!


Pavlos1316

Recommended Posts

...for me at least!  ;D

 

Hi there.

 

What I have:

 

1. I have - ready and working - my connection string to post in mysql db. - TESTED - Ok!

 

What I want:

 

1. Let's say my form is like:

                           

b1 b2 b3

                           

b4b5b6

                           

b7b8b9

 

1.a. Columns are used as check boxes.

 

2. The problem:

 

2.a. User MUST check 4 columns only - one in each row + a 2nd one in one of the rows.

 

2.b. When they check the 4th one, other columns must not be able to be checked.

 

2.c. User must not be able to check 4 columns in only 2 rows.

 

This may be the last step for my site but I have no idea how to do it!!!  ???

I hope you can help.

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/107801-heres-a-tought-one/
Share on other sites

Your asking someone to write a complete application for you basically and I doubt anyone has time.

 

I would probably start by making the rows form arrays and then you can check if more than one value is

in each row.

 

eg. row1 =

 

<input type="checkbox" name="a[1]" value="1" />

<input type="checkbox" name="a[2]" value="2" />

<input type="checkbox" name="a[3]" value="3" />

 

To check if more than one is selected in that row:

 

$a = $_POST["a"];

 

echo count( $a );

 

If the user checked 1 and 2 your count would be 2

 

So you know you can't have two on the next two arrays.

 

Time to get the thinking cap on ;)

 

Sorry, I don't have time to write the whole thing.

 

Link to comment
https://forums.phpfreaks.com/topic/107801-heres-a-tought-one/#findComment-552644
Share on other sites

Yes, it might be best to check in javascript first, however, what about

people without javascript enabled?

 

Like me, who run NoScript in Firefox. (Just to be awkward)

 

Using a combination of checking _POST and storing in _SESSION is would be

possible to do what they want.   I would check the POST, if its valid store in in the

SESSION, reverting to the SESSION version if the last POST contained an

invalid selection.

 

It's probably simpler in Javascript, and my first notion was to respond saying use

javascript, but is probably best to use both or just stick with a PHP version.

Link to comment
https://forums.phpfreaks.com/topic/107801-heres-a-tought-one/#findComment-552686
Share on other sites

Here is a javascript solution. Of course you SHOULD create the same functionality in PHP in case users have JS disabled - the same logic will apply. But, for situations like these I think the addition of Javascript is a definite benefit:

 

I'm sure this could be done more efficiently, but based upon the time I was willing to invest in this it does work.

<html>
<head>
<script type="text/javascript">
  function validateCheck() {
    formObj = document.getElementById('grid');

      var totalCount = 0;
      var rowCount = new Array();
      rowCount[0] = 0;
      rowCount[1] = 0;
      rowCount[2] = 0;

      for (var i=1; i<=9; i++) {

        if (formObj['B'+i].checked) {
          totalCount++;
          rowCount[(Math.ceil(i/3)-1)]++;
        }
      }

      var disableState = new Array();
      disableState[0] = false;
      disableState[1] = false;
      disableState[2] = false;

      //Determine rows to disable
      if (totalCount==4) {
        disableState[0] = true;
        disableState[1] = true;
        disableState[2] = true;
      } else {
        if (rowCount[0]==2 || (rowCount[0]==1 && (rowCount[1]==2 || rowCount[2]==2))) {
          disableState[0] = true;
        }
        if (rowCount[1]==2 || (rowCount[1]==1 && (rowCount[0]==2 || rowCount[2]==2)))
        {
          disableState[1] = true;
        }
        if (rowCount[2]==2 || (rowCount[2]==1 && (rowCount[0]==2 || rowCount[1]==2)))
        {
          disableState[2] = true;
        }
      }

      //Disable/enable unchecked boxes
      for (var i=1; i<=9; i++) {
        if (!formObj['B'+i].checked) {
          formObj['B'+i].disabled = disableState[(Math.ceil(i/3)-1)];
        }
      }


    return;
  }
</script>
</head>
<body>
<form name="grid" id="grid">
<table border="1">
  <tr>
    <td><input type="checkbox" id="B1" name="B1" value="B1" onclick="validateCheck();" />B1</td>
    <td><input type="checkbox" id="B2" name="B2" value="B2" onclick="validateCheck();" />B2</td>
    <td><input type="checkbox" id="B3" name="B3" value="B3" onclick="validateCheck();" />B3</td>
  </tr>
  <tr>
    <td><input type="checkbox" id="B4" name="B4" value="B4" onclick="validateCheck();" />B4</td>
    <td><input type="checkbox" id="B5" name="B5" value="B5" onclick="validateCheck();" />B5</td>
    <td><input type="checkbox" id="B6" name="B6" value="B6" onclick="validateCheck();" />B6</td>
  </tr>
  <tr>
    <td><input type="checkbox" id="B7" name="B7" value="B7" onclick="validateCheck();" />B7</td>
    <td><input type="checkbox" id="B8" name="B8" value="B8" onclick="validateCheck();" />B8</td>
    <td><input type="checkbox" id="B9" name="B9" value="B9" onclick="validateCheck();" />B9</td>
  </tr>
</table>
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/107801-heres-a-tought-one/#findComment-552718
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.