Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to programming, I have created a form with several check boxes and text fields. Each text field will activated if the check box is selected. The intersect key array is used to get all the checked check-box names and text field values. I want to insert these array data into a database table.

Can anyone give me some codes to do this. Thanks :)

This is function.php file

    global $usedTexts;
    $usedTexts = array();

    function postdata(){
        if ( isset($_POST['submit']) && array_key_exists("t", $_POST)  && is_array($_POST["t"]) && array_key_exists("cb", $_POST) && is_array($_POST["cb"])) {
             $usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);
        }
    }

and this is index.php file

  <?php include_once("function.php"); ?>
  <?php postdata(); ?>

  <form action="" method="post">
      <input type="checkbox" name="cb[1]" value="" onclick="document.getElementById('t1').disabled=!this.checked;" />
      <input type="number" max="5" min="1" id="t1" name="t[1]" value="" disabled="disabled" /> <br /><br />

      <input type="checkbox" name="cb[2]" value="" onclick="document.getElementById('t2').disabled=!this.checked;"/>
      <input type="number" max="5" min="1"id="t2" name="t[2]" value="" disabled="disabled" /><br /><br />

      <input type="checkbox" name="cb[3]" value="" onclick="document.getElementById('t3').disabled=!this.checked;"/>
      <input type="number" max="5" min="1"id="t3" name="t[3]" value="" disabled="disabled" /><br /><br />

      <input type="checkbox" name="cb[4]" value="" onclick="document.getElementById('t4').disabled=!this.checked;"/>
      <input type="number" max="5" min="1"id="t4" name="t[4]" value="" disabled="disabled" /><br /><br />

      <input name="submit" type="submit" value="Submit" />
  </form>
share|improve this question
    
Can you share your table structure? –  Krish R Nov 16 '13 at 19:50
    
table name is mysubjects and columns are subjectId (primary key) and subjectExp. checkbox values should insert into subjectId and text field values should go to subjectExp. Thanks :) –  PDS Nov 16 '13 at 19:55
add comment

1 Answer

up vote 1 down vote accepted

Can you try this,

      if ( isset($_POST['submit']) && array_key_exists("t", $_POST)  && is_array($_POST["t"]) && array_key_exists("cb", $_POST) && is_array($_POST["cb"])) {
        $usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);

                foreach($usedTexts as $subjectId=>$subjectExp){

                    $query=  "insert into mysubjects (subjectId, subjectExp) values('".$subjectId."','".$subjectExp."')";
                    //execute your query, before that make sure you have established the database connection
                }               

      }
share|improve this answer
    
i want to insert data into subjectId column also from the array –  PDS Nov 16 '13 at 20:11
    
subjectId added from array, please check –  Krish R Nov 16 '13 at 20:14
    
Thank you very much. :) –  PDS Nov 16 '13 at 20:18
    
NP. Glad to help you. –  Krish R Nov 16 '13 at 20:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.