Updating when a checkbox is ticked

Discussion in 'PHP' started by Kain, Oct 24, 2008.

  1. #1
    I have a form that extracts data from a mysql database and if a checkbox called update is checked it updates the database.

    The problem is that if the update field is a checkbox the script updates the first record set regardless of whether the checkbox is checked or not and does not update the last record.

    If I use a select menu instead of a checkbox it works fine.

    Heres the relevant bits of the code:
    <input name="update[]" type="checkbox" id="update" value="1">

    for($i=0;$i<$count;$i++){
    if($update[$i] ==1){

    // do stuff ###DOES NOT WORK

    *********************************************

    <select name="update[]" id="update">
    <option value="0" selected>No</option>
    <option value="1">Yes</option>
    </select>


    for($i=0;$i<$count;$i++){
    if($update[$i] ==1){

    // do stuff ###WORKS FINE

    Any ideas?

    Thanks
     
    Kain, Oct 24, 2008 IP
  2. salahsoftware

    salahsoftware Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    your code seems to be fine.
     
    salahsoftware, Oct 24, 2008 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    is this a checkbox or a dropdown?
    js or ajax?

    use onchange event or onSelect on every checkbox that has the same name,
    remeber to put a parameter
    onchange="yourFunction(this.value)"

    then catch it with $_REQUEST['checkboxname'];
    then do your query here
     
    bartolay13, Oct 24, 2008 IP
  4. Kain

    Kain Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The form is output by php as a standard html form.

    When the update field is a checkbox it does not work but when it's a select it works fine.

    PHP seems to be handling checkbox arrays differently than the text field arrays or the select box arrays.
     
    Kain, Oct 24, 2008 IP