1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What would be the best way to do this using php

Discussion in 'PHP' started by mumfry, Dec 26, 2014.

  1. #1
    Hello
    I will get right to it.

    I have a form like this
    <form>
    <div class="section2">
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    </div>
    
    <div class="section2">
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    </div>
    </form>
    HTML:
    Then i use php to loop through all the check-boxes of each section for the ones that are checked save it as check and if not checked save it to database as not checked
    foreach($_POST['checkboxes'] as key=>value)
    {
    $checked_box= (isset(value) ? "checked" : "notchecked");
    //then here code to save results(above checked_box variable) for each checkbox  in database
    }
    PHP:
    But the problem i am having with this is that, let say I went ahead and selected check-box number three of say section one for example, when ever it gets saved to the database, checkbox three ends up getting saved as unchecked, even though it was checked while checkbox one, the first checkbox get saved as selected, even though it was not selected.

    Whats really happening here, why does this happen.

    Any help on this would be greatly appreciated
    Thanks
     
    mumfry, Dec 26, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You would have to provide the rest of the code as well, for instance how you save stuff to the database. I'm guessing there is a wrongly assigned loop or value to be used in a loop, or something like that.
     
    PoPSiCLe, Dec 26, 2014 IP
  3. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #3
    This happens because only the checked checkboxes get sent while the unchecked checkboxes or not sent. What you can do instead is to add a value and check what the value of the checked checkbox is.

    
    <form>
    <div class="section2">
    <input type="checkbox" value="1" name="checkboxes[]"/>
    <input type="checkbox" value="2" name="checkboxes[]"/>
    <input type="checkbox" value="3" name="checkboxes[]"/>
    <input type="checkbox" value="4" name="checkboxes[]"/>
    </div>
    
    <div class="section2">
    <input type="checkbox" value="5" name="checkboxes[]"/>
    <input type="checkbox" value="6" name="checkboxes[]"/>
    <input type="checkbox" value="7" name="checkboxes[]"/>
    <input type="checkbox" value="8" name="checkboxes[]"/>
    </div>
    </form>
    
    Code (markup):
    And then check it like this.

    
    foreach($_POST['checkboxes'] as $key=>$value)
    {
    if ($value == 3) {
        //The third checkbox was checked!
    }
    //then here code to save results(above checked_box variable) for each checkbox  in database
    }
    
    PHP:
     
    Anveto, Dec 26, 2014 IP
    ThePHPMaster and HolyRoller like this.
  4. webshore88

    webshore88 Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #4
    Try this.

    <form>
    <div class="section2">
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    </div>
    <div class="section2">
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    <input type="checkbox" name="checkboxes[]"/>
    </div>
    </form>
    
    HTML:
    foreach($_POST['checkboxes'] as $key=>$value)
    {
    if(isset($_POST['checkboxes'][$key]))
    {
    // checkbox is checked
    }
    else
    {
    // checkbox is not checked
    }
    }
    PHP:
     
    webshore88, Jan 4, 2015 IP
  5. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    without a unique value identifying them it is just a bulk array and would have to be implode/explode
    better to give them separate IDs/value/name
     
    ezprint2008, Jan 12, 2015 IP
    MrPJH likes this.
  6. MrPJH

    MrPJH Well-Known Member

    Messages:
    1,066
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    155
    #6
    it is saving the first one as checked weather it is checked or not
    give them name and value
     
    MrPJH, Jan 13, 2015 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Why are you using DIV for obvious fieldsets, why are you assigning name attributes in a manner that you would have NO clue which one is which or from which section, (much less the uselessly vague classnames there...), lack of action or method...

    Your markup is incomplete gibberish, not certain what you would even expect to do with that mess server side much less how to go about it.
     
    deathshadow, Jan 21, 2015 IP