0

I have made a php form that is submitted by email. I am trying to make conditional check boxes so that if one specific check box is selected two others cannot be selected and if one or two of those two check boxes are selected the first one cannot. My code looks like this

<p>Ribeye Steak <input type="checkbox" name="Ribeye steak" value="Ribeye Steak"/> </p>
<p>Ribeye Roast (prime rib) <input type="checkbox" name="Ribeye Roast" value="ribeye roast"  /> </p>
<p>Sirloin Steak <input type="checkbox" name="Tenderloin roast" value="Tenderloin roast" /> </p>
<p>T-bone steak  <input type="checkbox" name="T-bone" value="T-bone steak" /> 
or New York Strip steak  <input type="checkbox" name="NY" value="New York Strip" /> 
and/or Tenderloin Filets <input type="checkbox" name="Filets" value="Filets steaks" /> </p>
<?php
if($_GET['T-bone steak'] == 'T-bone steak' AND $_GET['New York Strip'] == 'New York Strip') { 
          echo "You cannot select T-bone steak and NY Stip or Tenderloin. Please select T-bone or NY Strip and/or Tenderloin.";
          } else if($_GET['T-bone steak'] == 'T-bone steak' AND $_GET['Filets steaks'] == 'Filets steaks'){
          echo "You cannot select T-bone steak and NY Stip or Tenderloin. Please select T-bone or NY Strip and/or Tenderloin.";
          }
          else{
          }
?>

When the file is opened nothing happens. It still allows me to select any or all three of the checkboxes. Any ideas? Thank a bunch.

3 Answers 3

3

You need to use RADIO BUTTONS and group them rather than doing it by checkboxes.

Radio Buttons inherently support the feature of "when one is selected two others cannot be selected".

Sign up to request clarification or add additional context in comments.

Comments

1

The following code should do it. It uses jQuery for handling the checkboxes' state.

I tested it in Firefox 3.6.8, Chrome 6.0.472.53 beta and IE 8.0.7600.16385. It's working for me in all of them.

<html> 
<head> 

<script src="http://code.jquery.com/jquery-latest.js"></script>

<title>My Page</title>

<script type="text/javascript">
 $(document).ready(function()
 {
     $('input:checkbox').click(function()
     {
            if ($('#T-bone').is(':checked'))
            {
                $('#NY').attr("disabled", true);
                $('#Filets').attr("disabled", true);
            }
            else
            {
               $('#NY').attr("disabled", false);
               $('#Filets').attr("disabled", false); 
            }

            if($('#NY').is(':checked') || $('#Filets').is(':checked'))
            {
                $('#T-bone').attr("disabled", true); 
            }
            else
            {
               $('#T-bone').attr("disabled", false); 
            }
     });
});
</script>

</head> 

<body> 

<p>Ribeye Steak
<input type="checkbox" name="Ribeye steak" value="Ribeye Steak"/>
</p>

<p>Ribeye Roast (prime rib)
<input type="checkbox" name="Ribeye Roast" value="Ribeye Roast"  />
</p>

<p>Sirloin Steak
<input type="checkbox" name="Tenderloin roast" value="Tenderloin roast" />
</p>

<p>T-bone steak
<input type="checkbox" id="T-bone" name="T-bone" value="T-bone steak" /> 
or New York Strip steak
<input type="checkbox" id="NY" name="NY" value="New York Strip" /> 
and/or Tenderloin Filets
<input type="checkbox" id="Filets" name="Filets" value="Filets steaks" />
</p>

</body>

</html>

Edit:

Make sure you copy the whole script tag.

Currently your page has this (you're missing the line $(document).ready(function() ):

<script type="text/javascript">
{
     $('input:checkbox').click(function()
     {
          if ($('#T-bone').is(':checked'))
          {
               $('#NY').attr("disabled", true);
                        $('#Filets').attr("disabled", true);
          }
          else
          {
               $('#NY').attr("disabled", false);
               $('#Filets').attr("disabled", false); 
          }

          if($('#NY').is(':checked') || $('#Filets').is(':checked'))
          {
               $('#T-bone').attr("disabled", true); 
          }
          else
          {
               $('#T-bone').attr("disabled", false); 
          }
     });
});
</script>

It should have this:

<script type="text/javascript">
 $(document).ready(function()
 {
     $('input:checkbox').click(function()
     {
            if ($('#T-bone').is(':checked'))
            {
                $('#NY').attr("disabled", true);
                $('#Filets').attr("disabled", true);
            }
            else
            {
               $('#NY').attr("disabled", false);
               $('#Filets').attr("disabled", false); 
            }

            if($('#NY').is(':checked') || $('#Filets').is(':checked'))
            {
                $('#T-bone').attr("disabled", true); 
            }
            else
            {
               $('#T-bone').attr("disabled", false); 
            }
     });
});
</script>

9 Comments

I like where your going with this. However, when I inserted the code it still allows me to check T-bone and NY Strip or Filets
What browser are you using? Have you copied this whole code block and pasted into your .php file?
See the browser versions I used to test it: Firefox 3.6.8, Chrome 6.0.472.53 beta and Internet Explorer 8.0.7600.16385. It can be that for older browser versions the above code won't work as expected. Note: browser incompatibility usually happens in web development. If this is the case the above code must be adapted (a boring task).
I have the latest version of firefox 3.6.8. And I put the JQuery code in the php page that the form is in
You should copy and paste all the code I showed in my answer, not only the jQuery code. I added Id attributes in the checkbox input elements - like this: <input type="checkbox" id="T-bone" name="T-bone" value="T-bone steak" />.
|
1

The .php files can't be opened directly in the browser, like the .html ones. PHP is a server-side language, which means that it needs a server on which the php file should run. You have to install a virtual server on your machine. If you have Windows installed on it, you can use WAMP, which is a free program for creating a PHP and Apache virtual server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.