Jump to content

Post only checked items from list in form...


holfv

Recommended Posts

Hi. I've a form with subform (it cotains all records).

I want to check and register only checked rows.

The list items code is:

                                       <table id="bootstrap-data-table-export" class="table table-striped table-bordered">
                                          <thead>
                                             <tr>
                                                <th>ADD</th>
                                                <th>CODE</th>
                                                <th>COST</th>
                                             </tr>
                                          </thead>
                                          <tbody>
                                             <?php     
                                                $sql ="SELECT CODE,COST FROM PRODUCTS";
                                                $result = sqlsrv_query($conn, $sql);
                                                $count = 0;
                                                while ($row = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC))
                                                {?>
                                             <tr>                                                                                                                            
                                                <td><input type="checkbox" id="checkbox[<?php echo $row['CODE'];?>]" name="checkbox[<?php echo $row['CODE']; ?>]"></td>
                                                <td id="CODE" name="tableRow[<?php echo $count; ?>]

" value="<?php echo $row['CODE']);?>"><?php echo $row['CODE'];?></td>
                                                <td id="COST" name="tableRow[<?php echo $count; ?>][COST]" value="<?php echo $row['COST']);?>"><?php echo $row['COST'];?></td>
                                             </tr>
                                             <?php }?>
                                          </tbody>
                                          <?php $count++; ?>
                                       </table>

The code of the post is:

$count = $_POST['count'];

$tableRow = $_POST['tableRow'];
foreach($tableRow as $row){
/* here insert the selected items (in checkbox) data from post */
$id = $row['code'];
$cost = $_POST["cost"][$i];
$sentencia= "INSERT INTO LINES(code,cost) VALUES (?,?);";
$params1 = array($code,$cost);                     
$result = sqlsrv_query($conn,$sentencia,$params1);

}

 

I think that I need a loop but I don't know if the array is OK. The post file doesn't receive nothing. I only want to register the records checked. I would greatly appreciate any kind of help.

 

Link to comment
Share on other sites

2 hours ago, holfv said:

a form with subform

nested forms are invalid.

2 hours ago, holfv said:

The post file doesn't receive nothing

what is the form markup?

only checked checkboxes are included in the submitted form data. since you are using $row['CODE'] as the checkbox's array name index, that's all you need. you would just get and use the array indexes inside the post method form processing code. the checkboxes are the only valid form fields in the posted code. the rest of that isn't valid, but isn't needed anyways. you should only display the cost on the web page. you should not pass the cost through the web page, where it can be manipulated and be set to anything. you should get the cost in the post method form processing code if you need it. you would only store the cost with the selected items if the cost can change over time and you haven't setup a table to hold the cost history data.

you don't need id attributes in the markup unless you are referencing the individual elements in the browser. ids must also be unique, therefore the use of the same id='CODE' and id='COST' inside the loop doesn't work properly.

the post method form processing should detect is a post method form was submitted, before referencing any of the form data.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.