SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Getting values from multiple checkboxes

    I have this checkboxes

    HTML Code:
    <input type="checkbox" class="calc" name="access[a]" value="1" />
    <input type="checkbox" class="calc" name="access[b]" value="2" />
    <input type="checkbox" class="calc" name="access[c]" value="3" />
    <input type="checkbox" class="calc" name="access[d]" value="4" />
    I need the following thing, i will explain it in an example:
    Example: User selects third and fourth fields, 7(3+4) and cd ([access[c]+access[d]) will be send.
    I want 2 things to be send, calculation of value and access[values].

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Welcome to the forums

    When you say "send", do you mean you want to fie off an AJAX request, or do you want to submit the form?

    It shouldn`t be too hard to implement, but we need to know a bit more about what you are trying to do.

  3. #3
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Submit with Ajax

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Ok. What code have you got already?

  5. #5
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the code I use for submit:
    PHP Code:
    function privileges(){
    $.
    ajax({
    type:'POST',
    url:'./core/privileges.php',
    data:"access="+form_accessvalues+"values="+form_values,
    success:function(response){
    if(
    response=='1'){$('#privileges_label').css({'display':'block','border':'1px solid #bfde84','background':'#edfbd8 url(../images/success.gif) 12px 12px no-repeat','color':'#508600'}).html('Успешно заредихте 100 аммо')}
    else if(
    response=='2'){$('#privileges_label').css({'display':'block','border':'1px solid #e9c59b','background':'#ffecce url(../images/error.gif) 12px 12px no-repeat','color':'#e3302c'}).html('Моля, изберете правата които искате да закупите.')}
    else if(
    response=='3'){$('#privileges_label').css({'display':'block','border':'1px solid #e9c59b','background':'#ffecce url(../images/error.gif) 12px 12px no-repeat','color':'#e3302c'}).html('Моля, излезте от игра.')} 
    else if(
    response=='4'){$('#privileges_label').css({'display':'block','border':'1px solid #e9c59b','background':'#ffecce url(../images/error.gif) 12px 12px no-repeat','color':'#e3302c'}).html('Нямате достатачно кредити.')} 
    }
    });

    form_values = calculation of value
    form_accessvalues = access[flags]

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Ok. And what should happen when the user checks three or four or no boxes?
    Should the value still be calculated and the total value plus name attributes still be sent to the PHP script?

  7. #7
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Ok. And what should happen when the user checks three or four or no boxes?
    Should the value still be calculated and the total value plus name attributes still be sent to the PHP script?
    if no boxes are selected,both values and access[values] will be null.
    If at least 1 checkbox is selected values and access[values] from that/these will be send else values and acess[values] will be send as null.
    My english is not very good, I hope you have understand me.

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by notapoliceman View Post
    My english is not very good, I hope you have understand me.
    No problems. It's better than my Bulgarian

    What I would do in your case then, is to attach an event listener to the checkboxes, which fires whenever any of them are selected or deselected. You seem to be using jQuery, so I'll go with that:

    Code:
    <input type="checkbox" class="calc" name="access[a]" value="1" />
    <input type="checkbox" class="calc" name="access[b]" value="2" />
    <input type="checkbox" class="calc" name="access[c]" value="3" />
    <input type="checkbox" class="calc" name="access[d]" value="4" />
    
    $("input:checkbox").on("change", function(){
      privileges();
    })
    As you can see, this calls the privileges function.

    Within this function, we then need to work out what to pass to your PHP script:

    The most readable way to pass data via jQuery ajax() is to use an object literal:

    Code:
    data: {
      "access": getAccessValues(),
      "values": getFormValues(), 
     },
    And you can assign the return value of a function call to these variables.

    Code:
    function getAccessValues(){
      var numCheckboxes = $(":checkbox:checked").length;
    
      if (numCheckboxes === 0){
         return whatever
      } else if (numCheckboxes === 1){
        return whatever
      }else {
        return whatever
      }
    }
    
    getFormValues(){
      var numCheckboxes = $(":checkbox:checked").length;
      ...
    }
    HTH


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •