holfv Posted October 24, 2023 Share Posted October 24, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317392-post-only-checked-items-from-list-in-form/ Share on other sites More sharing options...
requinix Posted October 24, 2023 Share Posted October 24, 2023 Start with the values in $_POST. That will tell you only the checkboxes that were checked. Then you find out what the corresponding costs are and add them to your table - something which you can do easily with an INSERT...SELECT query. Quote Link to comment https://forums.phpfreaks.com/topic/317392-post-only-checked-items-from-list-in-form/#findComment-1612539 Share on other sites More sharing options...
mac_gyver Posted October 24, 2023 Share Posted October 24, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317392-post-only-checked-items-from-list-in-form/#findComment-1612541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.