Jump to content

[SOLVED] insert array data only if check


TheJuan

Recommended Posts

hi

i have here a form which a result query from database. i would like to execute a script which insert to another table, those data with a check can only be inserted. my problem is that when i check the first 2, skip two data, and check the last data, i get the first 3 data. still the same when i check the 1st, skip 2nd,3rd,4th, check the last, i still get the first 2 data which is wrong. hope i make it clear.

 

here is the form

<form method='POST' action='add.php'>
<table border='0' width='100%' class='kylin'>
<tr>
    <th width='6%'>Add</td>
  </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO13' name='xsocode[]'></td>
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO15' name='xsocode[]'></td>      
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO17' name='xsocode[]'></td>  
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO71' name='xsocode[]'></td>
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO16' name='xsocode[]'></td>
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  </table>
<input type='submit' value='Submit' name='submit'>
<input type='hidden' value='set'name='pasalog' />
<input type='hidden' value='04-05607' name='xstudentno'>
<input type='hidden' value='vincent' name='user'>
</p>
</form>

 

here is my code for add.php

$vsocode = $_POST['xsocode'];
$vstudentno = $_POST['xstudentno'];
$vuser = $_POST['user'];
$vdate = date('m-d-Y');
$vtime = date('h:i:s A');
$kylin = $_POST['check_block'];

if (isset($_POST['pasalog'])) {
      foreach ($kylin as $key => $value) {
            $query = "
            INSERT INTO subjects_enrolled_tbl SET studentno = '$vstudentno', subjectenrolled = '$vsocode[$key]', encoder = '$vuser', dateentered = '$vdate', timeentered = '$vtime';
            ";
            print $key . "-" . $query . "<br/>";
            }
      }

Link to comment
https://forums.phpfreaks.com/topic/54235-solved-insert-array-data-only-if-check/
Share on other sites

Add a counter variable and specify the keys in the form names, so you get

 

<tr>
    <th width='6%'>Add</td>
  </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO13' name='xsocode[1]'></td>
      <td width='3%'><input type='checkbox' name='check_block[1]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO15' name='xsocode[2]'></td>      
      <td width='3%'><input type='checkbox' name='check_block[2]' value='check_block' checked></td>
      </tr>

 

Only checked boxes get posted, so if 2 are unchecked the key values get out of synch

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.