views:

54

answers:

3

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  A: 

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".

shamittomar
i did that but it still didn't work
Lars
+1  A: 

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.

Max
+1  A: 

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"&gt;&lt;/script&gt;

<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>
Leniel Macaferi
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
Lars
What browser are you using? Have you copied this whole code block and pasted into your .php file?
Leniel Macaferi
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).
Leniel Macaferi
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
Lars
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" />.
Leniel Macaferi
The code should work in your Firefox version.
Leniel Macaferi
I pasted the whole code. Here is the web site if you want to take a look at it spillvillelocker.com/beef.php
Lars
OK. I'll look. Let me just finish eating. :) It's not a joke. I'm not referring to beefs.
Leniel Macaferi
I checked your page online using Firebug for Firefox - http://getfirebug.com/. It shows that you copied the code wrongly.
Leniel Macaferi
This is the error the Console tab shows: syntax errorerror source line: [Break on this error] });\n - your script tag is malformed.
Leniel Macaferi
so what should i do?
Lars
I updated my answer with what you should do...
Leniel Macaferi
That worked!!! Thank you very much!
Lars
You're welcome. I'm here to help and I'm glad it worked. :)
Leniel Macaferi