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
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
Share on other sites

You're asking for Javascript help.  PHP only handles the backend, while Javascript handles dynamic frontends.  Also, we will not write the entire application for you.  If you want that, go to the Freelance board and be prepared to pay someone.

Link to comment
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
Share on other sites

 

I don't actually know what they are trying to achieve, so perhaps yes :)

 

I was just answering in the context of it being a PHP question.

 

Anyway, good luck to the poster, whether you decide on PHP or JS.

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.