views:

356

answers:

3

I just received some really great help today with a prior jQuery problem and figured since my luck was running that maybe I could also get some help with some checkboxes. Can someone please tell me what I am doing wrong?

Thanks!

The checkboxes are correctly echoing the database bool values but when I submit the changed values, an alert() telles me that they are undefined.

        else if (item.field == "admCustRptDly" && item.value == "1")
        {
          $('#admCustRptDly').attr('checked', true);
        }

        else if (item.field == "admCustRptSumm" && item.value == "1")
        {
          $('#admCustRptSumm').attr('checked', true);
        }

        else if (item.field == "admCustRptDtl" && item.value == "1")
        {
          $('#admCustRptDtl').attr('checked', true);
        }

<tr>
    <td class="admMarker">Daily<input type="checkbox" id="admCustRptDly" name="admCustRptDly" class="admChkbx"></td>
    <td class="admMarker">Summary<input type="checkbox" id="admCustRptSumm" name="admCustRptSumm" class="admChkbx"></td>
    <td class="admMarker">Detail<input type="checkbox" id="admCustRptDtl" name="admCustRptDtl" class="admChkbx"></td>
</tr>


$(function() {   $('.error').hide();
    $('input.text-input').css({backgroundColor:"#FFFFFF"});
    $('input.text-input').focus(function(){
        $(this).css({backgroundColor:"#FFDDAA"});
    });  
    $('input.text-input').blur(function(){
        $(this).css({backgroundColor:"#FFFFFF"});
    });

      $(".admCustBtn").click(function()
    {    // validate and process form
         // first hide any error messages
        $('.error').hide();



          var admCustRPSecPhone =
    $("input#admCustRPSecPhone").val();
          var admCustRptDly =
    $("checkbox#admCustRptDly").val();  
    var admCustRptSumm =
    $("checkbox#admCustRptSumm").val();
          var admCustRptDtl =
    $("checkbox#admCustRptDtl").val();

         var dataString =
        'admCustID='+ admCustID +
        '&admCustRptDly='+ admCustRptDly +
        '&admCustRptSumm='+ admCustRptSumm +
        '&admCustRptDtl='+ admCustRptDtl;

         alert (dataString);return false;

         $.ajax({
          type: "POST",
          url: "body.php?action=admCustomer",
          data: dataString,
          success: function(){
            alert( "Success! Data Saved");
          }
         });
        return false;   }); });
A: 

You have no value attribute set on your HTML input element

<input type="checkbox" value="1" id="admCustRptDly" name="admCustRptDly">
duckyflip
Frank I am not talking about the JS part, check the example that I have posted, there is an extra value attribute there.
duckyflip
+1  A: 

Actually both..

the checkboxes don't have value, so if you try to alert() their values it will lead to "undefined", but if you are facing this on alerting the checkbox itself you are probably doing something wrong.

Setting their values to true, won't lead to anything, as @Soviut said, most properties repeat their names on setting. So your input will get like:

<input type="checkbox" checked="checked" value="1" name="myCheck" />

So, try the above and give us some feedback =´p

José Leal
Frank I honestly hope you've made a mistake while typing this comment because you actually ought to add the value=1 to your <input> elements not <td>
duckyflip
Sorry Jose, I'm so excited about the possibility of getting this to actually work that my brain is running faster than my fingers. :)Yes, I added value="1" to my input tag.
Wait, why are you using checkboxes if you only need to get 1 value at the time? If you want to check if they are checked or not, use document.getElementById('yourCheckboxId').checked which will give you a bool value if yes or no
José Leal
I'm still getting undefined even after adding checked="checked" value="1" to the imput tag.
No, I need all 3 or none. It isn't only one value. These are reports that the customer can select. They have an option to opt in to all 3 or none.
Ok, put the entire page in your question, so we can help you.
José Leal
Wait, so use the radio, instead of checkboxes! <input type="radio" value="1" name="myName" />, just use the same name for all of them changing their values, and check their content by document.getElementsByName('myName').value
José Leal
Damn. I wish I knew that you answered me. Sometimes stack overflow is a pain to use. I'm sorry Jose but I was very unclear above. The user has the choice of selecting any one of the three. Bottom line is that the radio button can't be grouped.
A: 

Your selectors for the checkboxes are not correct.

  var admCustRPSecPhone = $("input#admCustRPSecPhone:checked").val() == 'on';
  var admCustRptDly = $("input#admCustRptDly:checked").val() == 'on';  
  var admCustRptSumm = $("input#admCustRptSumm:checked").val() == 'on';
  var admCustRptDtl = $("input#admCustRptDtl:checked").val() == 'on';

You could also use something like:

  var admCustRptDly = $("#admCustRptDly:checkbox:checked").val() == 'on';

This will set the values to true/false depending on whether the checkbox is checked or not.

tvanfosson
tvanfosson, is it possible to have these checkboxes send a "0" for false and a "1" for true? I guess I don't understand because when I remove the value="1" attribute from the input tag, the current bool values will show. When I add the value="1", the checkboxes are STILL correctly showing the current database values BUT.. when I submit the form, all of the checkboxes are showing checked, whether or not they in fact are. Hope that makes sense.
@Frank - select only the checked ones. I've updated my answer to reflect this.
tvanfosson
tvanfosson, thanks for sticking with me on this. I am making those changes now but I guess I am wondering how the database will accept the new values. I mean, the datatype in the database is set to tinyint so it is looking for an int value.
tvanfosson, couldn't I just server side to make the conversion? Is this the best way of doing it?
Sure, or you could add "? 1 : 0", using the ternary operator, to the end of each assignment and send them as 1s and 0s.
tvanfosson
Use a ternary operator in JQuery?In my class method, I added a ternary operator which works but I would *rather* if possible do it with JQ. If not, no big deal. So was that what you meant about using the ternary operator? In JQ or PHP?
You should be able to use it in the javascript. var x = $('#name:checkbox:checked').val() == 'on' ? 1 : 0;
tvanfosson