Problem with checkbox value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semanticnotion
    New Member
    • Sep 2010
    • 66

    Problem with checkbox value

    I have 5 checkboxes in HTML page and i want to save the value of checked checkbox into mysql using PHP but when i save the form in database the value of checkbox is 1 while the value of checkbox is a,b,c,d,e etc i used the type of checkbox field as text but it saves 1.

    Here is my code
    Code:
    <input type="checkbox" name="option[]" value="a"/>
    <input type="checkbox" name="option[]" value="b"/>
    <input type="checkbox" name="option[]" value="c"/>
    <input type="checkbox" name="option[]" value="d"/>
    <input type="checkbox" name="option[]" value="e"/>
    <input type="submit" name="submit" value="save" />
    The php code is
    Code:
    echo"<br />";
    if(isset($_POST['submit']))
    {
    for ($i=0; $i<count($_POST['option']);$i++) {
    $i = $_POST['option'][$i];
    echo "the check box selected is $i";
    echo"<br />";
    }
    }
    
    
    $query3="INSERT INTO solution VALUES('$i','$question_id')";
    
    $result3=mysql_query($query3) or die(mysql_error());
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    try the link

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      First of all, your code to echo the checkboxes will fail miserably considering you change the value of $i inside of the loop.

      Secondly, when your form is post, only checked checkboxes are posted. So, you can check if a checkbox has been posted by checking if it isset() in the $_POST array.

      Thirdly, your SQL query doesn't make sense at all.

      Comment

      Working...