Inserting checkbox values into database

前端 未结 6 1369
醉梦人生
醉梦人生 2020-12-10 23:39

I need help for this problem that i\'m trying to solve for a while (i\'m new in PHP). I have a form with several checkboxes which values are pulled from a database. I manage

相关标签:
6条回答
  • 2020-12-10 23:43

    You can tell if a checkbox is selected because it will have a value. If it's not selected, it won't appear in the request/get/post in PHP at all.

    What you may want to do is check for the value of it and work based on that. The value is the string 'on' by default, but can be changed by the value='' attribute in HTML.

    Here are a couple snippets of code that may help (not exactly production quality, but it will help illustrate):

    HTML:

    <input type='checkbox' name='ShowCloseWindowLink' value='1'/> Show the 'Close Window' link at the bottom of the form.
    

    PHP:

    if (isset($_POST["ShowCloseWindowLink"])) {
        $ShowCloseWindowLink=1;
    } else {
        $ShowCloseWindowLink=0;
    }
    
            .....
    
    
    $sql = "update table set ShowCloseWindowLink = ".mysql_real_escape_string($ShowCloseWindowLink)." where ..."
    

    (assuming a table with a ShowCloseWindowLink column that will accept a 1 or 0)

    0 讨论(0)
  • 2020-12-10 23:46

    2nd Answer:

    You might do something like this:

    HTML:

    echo '<input type="checkbox" name="id_dodatoci[]" value="'.$id_dodatoci.'" />';
    

    PHP:

    if ( !empty($_POST["id_dodatoci"]) ) {
        $id_dodatoci = $_POST["id_dodatoci"];
        print_r($id_dodatoci);
        // This should provide an array of all the checkboxes that were checked.
        // Any not checked will not be present.
    } else {
        // None of the id_dodatoci checkboxes were checked.
    }
    

    This is because you are using the same name for all of the checkboxes, so their values will be passed to php as an array. If you used different names, then each would have it's own post key/value pair.

    This might help too:

    http://www.php-mysql-tutorial.com/php-tutorial/using-php-forms.php

    0 讨论(0)
  • 2020-12-10 23:49

    Also something that few people use but that is quite nice in HTML, is that you can have:

    <input type="hidden" name="my_checkbox" value="N" />
    <input type="checkbox" name="my_checkbox" value="Y" />
    

    and voila! - default values for checkboxes...!

    0 讨论(0)
  • 2020-12-10 23:52

    Hoi!

    Well, as Eli wrote, the POST is not set, when a checkbox is not checked.

    I sometimes use an additional hidden field (-array) to make sure, I have a list of all checkboxes on the page.

    Example:

    <input type="checkbox" name="my_checkbox[<?=$id_of_checkbox?>]">
    <input type="hidden" name="array_checkboxes[<?=$id_of_checkbox?>]" value="is_on_page">
    

    So I get in the $_POST:

    array(2){
     array(1){"my_checkbox" => array(1){[123]=>"1"}}
     array(1){"array_checkboxes" => array(1){[123]=>"is_on_page"}}
    }
    

    I even get the second line, when the checkbox is NOT checked and I can loop through all checkboxes with something like this:

    foreach ($_POST["array_checkboxes"] as $key => $value)
    {
      if($value=="is_on_page")
      {
        $value_of_checkbox[$key] = $_POST["my_checkbox"][$key];
        //Save this value
      }
    }
    

    Hope this helps! :)

    Best regards, Bastian

    0 讨论(0)
  • 2020-12-10 23:59

    As an extra note: You're using the wrong HTML syntax for IDs and <label>. <label>'s "for" attribute should point to an ID, not a value. You also need unique IDs for each element. The code you have posted would not validate.

    Also, you're not validating your code at all. At the very least, do a htmlspecialchars() or htmlentities() on the input before you output it and a mysql_real_escape_string() before you insert data into the DB.

    0 讨论(0)
  • 2020-12-11 00:00

    This is the loop that I needed. I realized that I need a loop through each key with the $i variable.

    if(isset($_POST['id_dodatoci'])){
        $id_dodatoci=$_POST['id_dodatoci'];
        $arr_num=count($id_dodatoci);
        $i=0;
        while ($i < $arr_num)
        {
            $query="INSERT INTO `dodatoci_hotel`(id_dodatoci,info_id) 
                VALUES ('$id_dodatoci[$i]','$info_id')";
            $res=mysql_query($query) or die('ERROR INSERTING: '.mysql_error());
            $i++;
        }
    }
    
    0 讨论(0)
提交回复
热议问题