checkbox problem in php

Discussion in 'PHP' started by MSK7, Feb 17, 2009.

  1. #1
    Hi to all,

    actually i have a simple form called "form2" in which,
    i have used some checkboxes to choose users several values of "Hobbies".

    So my problem arises that,
    if a person selects any one of the hobby , and submit form
    then the value of that single hobby is properly entered into the database,
    But if he selects more than one hobbies then also the values of
    any of them one single value is entered in to the database.
    rather than his selected all multiple values of hobbies.

    what should be done so that All the (multiple hobby) values selected by a person properly enter into the database?

    its validation is shown below:-

    <script type="text/javascript">

    function validate_required(form2)
    {
    if (!form2.checkbox[0].checked && !form2.checkbox[1].checked && !form2.checkbox[2].checked )
    {
    alert("Please Select any Hobby");
    return false;
    }
    return true;
    }

    </script>

    & here its code is as follows:-

    <form name="form2" onSubmit="return validate_required(this)" action="submit2.php" method="post" >


    Hobbies : <input type = "checkbox" name="checkbox" id= "checkbox1" value="movies"> Movies.

    <input type = "checkbox" name="checkbox" id= "checkbox2" value="reading">Reading.

    <input type = "checkbox" name="checkbox" id= "checkbox3" value="sports"> Sports.


    please suggest me the right code to be used or any other suggessions regarding this.

    Thanks & Regards.
     
    MSK7, Feb 17, 2009 IP
  2. vijaybhaskar184

    vijaybhaskar184 Member

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    For a check box you have to use differetn name attributes for each check box or you can use any array to work in php. use some thing like
    <input type = "checkbox" name="checkbox1" id= "checkbox2" value="reading">Reading.

    <input type = "checkbox" name="checkbox2" id= "checkbox3" value="sports"> Sports.
     
    vijaybhaskar184, Feb 17, 2009 IP
  3. MSK7

    MSK7 Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Hello sir,
    first of all thanks for your precious suggession.

    please suggest me what changes wll be applied to my "submit2.php" form according to your code.

    "submit2.php" :-


    <?php

    $sql="INSERT INTO mydatabase.mytablename (Name, Age, Hobbies, )
    VALUES
    ('$_POST[name]','$_POST[age]','$_POST[checkbox]')" ;

    ?>

    Thanks & Regards.
     
    MSK7, Feb 17, 2009 IP
  4. vijaybhaskar184

    vijaybhaskar184 Member

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Its better to use an array for check box name. Ex: name="checkbox[0]" and so on
    once you had used any array
    then you can retrieve the array values as $_POST['checkbox[$i]'] using a loop.
    and insert it into your database
     
    vijaybhaskar184, Feb 17, 2009 IP
  5. MC_delta_T

    MC_delta_T Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    change checkboxes name checkbox to checkbox[]. you dont have to write keys. and
    print_r($_POST['checkbox']);
    PHP:
     
    MC_delta_T, Feb 17, 2009 IP