0

So i have a table, that I load checkbox values into. Once the checkboxes are clicked, I use a submit button to save them into a database. However at the moment I can only Save one row at a time? I need to be able to save all clicked checkboxes into the datbase

For example i have 5 rows. If the checkboxes on row 2 and 3 are checked then they should be saved into the database however right now only the last clicked(row 3) are being saved into the database. Heres my code so far

Php connect code

<?php
   // Get a db connection.
   $db = JFactory::getDbo();

   // Create a new query object.
   $query = $db->getQuery(true);


   $query->select($db->quoteName(array('CV_ID', 'Classifier', 'Value')));
   $query->from($db->quoteName('classvalues'));

   // Reset the query using our newly populated query object.
   $db->setQuery($query);

   // Load the results as a list of stdClass objects (see later for more options on retrieving data).
   $results = $db->loadObjectList();
 ?>

Loading table with checkboxes code

<form name="names" id="names" action="<?php echo JURI::current(); ?>" method="post"> 
   <table border="5",th,td, cellspacing="5", cellpadding="5", width="500", align="left">
     <tr>
       <th>CV_ID</th>
       <th>Classifier</th>
       <th>Level</th>
     </tr>

     <?php foreach ($results as $row): ?>

     <tr>
      <td> <input type="checkbox" id="chk113" name="CV_ID" value="<?php echo $row->CV_ID ?> "/>
      <label for="chk113"><?php echo $row->CV_ID ?> </label> </td>

      <td> <input type="checkbox" id="chk111" name="Classifier" value="<?php echo $row->Classifier ?>"/>
      <label for="chk111"><?php echo $row->Classifier ?></label> </td>

      <td> <input type="checkbox" id="chk112" name="Value" value="<?php echo $row->Value ?>"/>
      <label for="chk112"><?php echo $row->Value ?></label> </td>
    </tr>

    <?php endforeach ?>
  </table>

    <p><input id="submit" name="submit" type="submit" value="Submit Names" /></p>
 </form>

Saving ?>

 <?
   if( (isset($_POST['CV_ID'])) || (isset($_POST['Classifier'])) || (isset($_POST['Value'])) ) {
   //first name or last name set, continue-->
   $CV_ID = $_POST['CV_ID'];
   $Classifier= $_POST['Classifier'];
   $Value= $_POST['Value'];

   $db =& JFactory::getDBO();
   $query = "INSERT INTO SessionTa (CV_ID, Classifier, Value) VALUES ('".$CV_ID."','".$Classifier."','".$Value."');";


   $db->setQuery( $query );
   $db->query();

   } else {
   }
 ?>

1 Answer 1

0

each checkbox in your form appears to have the same name. Trying naming them as an array, e.g.

<input type="checkbox" id="chk113" name="CV_ID[]" value="<?php echo $row->CV_ID ?> "/>  

Notice the extra '[]'. At that point, you can print_r($_POST) in your saving php file to see the differences. Your db update logic might need to be tweaked as a result

Sign up to request clarification or add additional context in comments.

1 Comment

Comes up in my table as "array" Im just trying to insert multiples rows into my database table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.