Jump to content

only first checkbox selecting


I-AM-OBODO
Go to solution Solved by cyberRobot,

Recommended Posts

Hello all,

i have worked my head out with different solution but none have given me the desired result and so i have to seek help. I dont know why only the first check box is working. even when i select the last checkbox, the first checkbox will work.

Thanks

 

<?php

if(isset($_POST['delete'])){

    if(empty($_POST['check_list'])){
        echo "<div class='bg-warning alert alert-warning text-center margin-top'>YOU DID NOT MAKE ANY SELECTION</div>";
    }else{

    if(!empty($_POST['check_list'])){

            $update = $pdo->prepare("
                            UPDATE  tbl_stock
                            SET     stock_qty     = stock_qty + :stock_qty
                            WHERE   prod_id       = :prod_id

                            ");
            foreach($_POST['check_list'] as $k => $prod_ids){ 
			// if i use $prod_id it gives a weird result
			// i also did ':prod_id'    => $prod_id to no avail
            $update->execute( [ 
                                ':prod_id'    => $_POST['prod_id'][$k],
                                ':stock_qty'  => $_POST['prod_qty'][$k]
                              ] );

            $cnt = $update->rowCount();
        }

    if($cnt){

         $query = "
            UPDATE tbl_sales 
            SET  status=2, 
            date_deleted = NOW()
            WHERE trans_ref='$_SESSION[temp_trans_ref]'
            AND     prod_id       = :prod_id
 
            "; 


            $stmt = $pdo->prepare($query);
            foreach($_POST['check_list'] as $k => $prod_name){
            $stmt->execute([                         
                                ':prod_id'    => $_POST['prod_id'][$k]
                            ]);
            }


    echo "<div class='bg-success alert alert-success text-center text-white'>RECORD(S) DELETED</div>";

    }else{
        echo "<div class='bg-danger alert alert-danger text-center text-white'>RECORD NOT DELETED</div>";
       
    }
        }
    }
        }

?>

    <form action="" method="post">
    <div class="sales-form">

        <?php if( isset($error_msg)){ echo $error_msg; } ?>

        <table class='table'>
            <thead>
                <tr>
                    <th></th>
                    <th>Item Name</th> 
                    <th>Product Size/Type</th>
                    <th>Quantity</th>
                    <th>Price (₦)</th>
                    <th>Total (₦)</th>
                </tr>
            </thead>
          <tbody><tr>
        <?php

            $t_price = 0;
            $stmt = $pdo->query("
            SELECT * 
            FROM tbl_sales s
            LEFT JOIN  tbl_sales_total st ON s.trans_ref = st.trans_ref
            LEFT JOIN tbl_products p ON s.prod_id = p.prod_id
            WHERE s.trans_ref = '$_SESSION[temp_trans_ref]' AND s.status = 0
            ");
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)){

                $prod_id        = $row['prod_id'];
                $cost_price     = $row['cost_price'];
                $prod_name      = $row['prod_name'];
                $size_type      = $row['size_type'];
                $prod_qty       = $row['prod_qty'];
                $sold_price     = $row['sold_price'];
                $total_price    = $row['total_price'];
                $t_price += $row['total_price']; 
          
            ?>

             
                <td><input type='checkbox' name='check_list[]'      value='<?= $prod_id ?>' ></td>
                    <input type='text'     name='prod_id[]'       value='<?= $prod_id ?>' hidden>
                    <input type='text'     name='cost_price[]'      value='<?= $cost_price ?>' hidden>
                    <input type='text'     name='prod_name[]'       value='<?= $prod_name ?>' hidden>
                    <input type='text'     name='size_type[]'       value='<?= $size_type ?>' hidden>
                    <input type='text'     name='sold_price[]'      value='<?= $sold_price ?>' hidden>
                    <input type='text'     name='prod_qty[]'        value='<?= $prod_qty ?>' hidden>
                    <input type='text'     name='total_price[]'     value='<?= $total_price ?>' hidden>
                <td><?= $prod_name ?></td>
                <td><?= $size_type ?></td>
                <td><?= $prod_qty ?></td>
                <td><?= $sold_price ?></td>
                <td><?= $total_price ?></td>
                
             </tr>

    <?php

        }
        
    ?>

 

Link to comment
Share on other sites

20 minutes ago, Barand said:

Remember that only check checkboxes are posted, so the kth checkbox isn't necessarily the kth other items

I'd use the id as index for the post array, for example...

<input type='text'     name='prod_qty[$prod_id]'        value='<?= $prod_qty ?>' hidden>

 

thanks for your reply. i dont get you

Link to comment
Share on other sites

As Barand mentioned, only the checked checkboxes will be passed via $_POST. If you have 20 checkboxes and only 2 are checked, the array for $_POST['check_list'] will only have 2 elements...where the other arrays (e.g., $_POST['prod_id']) will have 20. So, the use of $k in the code above won't work since there's no guarantee the array indexes will line up between $_POST['check_list'] and the other arrays.

To get a better picture of what's going on after the form is submitted, you could add the debug statement below. That will show the information being submitted. Try checking one box in your form and hit submit.

echo '<pre>' . print_r($_POST, true) . '</pre>';

Adding the $prod_id, as shown by Barand above, is a good way to determine which checkbox corresponds with the other form fields.

<td><input type='checkbox' name='check_list[$prod_id]'    value='<?= $prod_id ?>' ></td>
    <input type='text'     name='prod_id[$prod_id]'       value='<?= $prod_id ?>' hidden>
    <input type='text'     name='cost_price[$prod_id]'    value='<?= $cost_price ?>' hidden>
...

 

Link to comment
Share on other sites

With that said, is there a reason for sending the hidden fields? I didn't spend too much analyzing the code, but it seems like you're just passing values that are already in the database.

If any of that information is needed after the form is submitted, you could use the ID from the checkbox to pull the necessary values from the database. That way you don't need to worry about someone potentially tampering with the "hidden" fields.

Link to comment
Share on other sites

1 hour ago, cyberRobot said:

As Barand mentioned, only the checked checkboxes will be passed via $_POST. If you have 20 checkboxes and only 2 are checked, the array for $_POST['check_list'] will only have 2 elements...where the other arrays (e.g., $_POST['prod_id']) will have 20. So, the use of $k in the code above won't work since there's no guarantee the array indexes will line up between $_POST['check_list'] and the other arrays.

To get a better picture of what's going on after the form is submitted, you could add the debug statement below. That will show the information being submitted. Try checking one box in your form and hit submit.

echo '<pre>' . print_r($_POST, true) . '</pre>';

Adding the $prod_id, as shown by Barand above, is a good way to determine which checkbox corresponds with the other form fields.

<td><input type='checkbox' name='check_list[$prod_id]'    value='<?= $prod_id ?>' ></td>
    <input type='text'     name='prod_id[$prod_id]'       value='<?= $prod_id ?>' hidden>
    <input type='text'     name='cost_price[$prod_id]'    value='<?= $cost_price ?>' hidden>
...

 

Thanks for the pointer. Now it only applies for the bottom checkbox regardless of the number of checkbox ticked.

 <td><input type='checkbox' name='check_list[$prod_id]'      value='<?= $prod_id ?>' ></td>
                    <input type='text'     name='prod_id[$prod_id]'         value='<?= $prod_id ?>' hidden>
                    <input type='text'     name='cost_price[$prod_id]'      value='<?= $cost_price ?>' hidden>
                    <input type='text'     name='prod_name[$prod_id]'       value='<?= $prod_name ?>' hidden>
                    <input type='text'     name='size_type[$prod_id]'       value='<?= $size_type ?>' hidden>
                    <input type='text'     name='sold_price[$prod_id]'      value='<?= $sold_price ?>' hidden>
                    <input type='text'     name='prod_qty[$prod_id]'        value='<?= $prod_qty ?>' hidden>
                    <input type='text'     name='total_price[$prod_id]'     value='<?= $total_price ?>' hidden>
                <td><?= $prod_name ?></td>
                <td><?= $size_type ?></td>
                <td><?= $prod_qty ?></td>
                <td><?= $sold_price ?></td>
                <td><?= $total_price ?></td>

 

 

Array
(
    [prod_id] => Array
        (
            [$prod_id] => 116154
        )

    [cost_price] => Array
        (
            [$prod_id] => 200.00
        )

    [prod_name] => Array
        (
            [$prod_id] => Bread
        )

    [size_type] => Array
        (
            [$prod_id] => By 10
        )

    [sold_price] => Array
        (
            [$prod_id] => 300.00
        )

    [prod_qty] => Array
        (
            [$prod_id] => 5
        )

    [total_price] => Array
        (
            [$prod_id] => 1500.00
        )

    [check_list] => Array
        (
            [$prod_id] => 116154
        )

    [delete] => 
)

 

with this, the value for bread (going by example: 5) is added to whichever checkbox is checked

foreach($_POST['check_list'] as $k => $prod_id){
            $update->execute( [ 
                                ':prod_id'    => $prod_id,
                                ':stock_qty'  => $_POST['prod_qty'][$k]
                              ] );

 

with this, the value for bread (going by example: 5) is added to only bread regardless of the checkbox checked

foreach($_POST['check_list'] as $k => $prod_id){
            $update->execute( [                
                                :prod_id'  => $_POST['prod_id'][$k],
                                ':stock_qty'  => $_POST['prod_qty'][$k]
                              ] );

Thanks

Edited by I-AM-OBODO
Link to comment
Share on other sites

  • Solution

Sorry about that, the IDs need to be enclosed in PHP tags. Here's an updated example:

<td><input type='checkbox' name='check_list[<?= $prod_id ?>]' value='<?= $prod_id ?>' ></td>
    <input type='text'     name='prod_id[<?= $prod_id ?>]'    value='<?= $prod_id ?>' hidden>
    <input type='text'     name='cost_price[<?= $prod_id ?>]' value='<?= $cost_price ?>' hidden>
...

 

  • Thanks 1
Link to comment
Share on other sites

1 hour ago, cyberRobot said:

With that said, is there a reason for sending the hidden fields? I didn't spend too much analyzing the code, but it seems like you're just passing values that are already in the database.

If any of that information is needed after the form is submitted, you could use the ID from the checkbox to pull the necessary values from the database. That way you don't need to worry about someone potentially tampering with the "hidden" fields.

No other reason. Its just so the value be available for the form. I don't want the table to tags to have form input inside them cos it will make it not look nice. How would you approach it?

Thanks

Link to comment
Share on other sites

50 minutes ago, cyberRobot said:

Sorry about that, the IDs need to be enclosed in PHP tags. Here's an updated example:

<td><input type='checkbox' name='check_list[<?= $prod_id ?>]' value='<?= $prod_id ?>' ></td>
    <input type='text'     name='prod_id[<?= $prod_id ?>]'    value='<?= $prod_id ?>' hidden>
    <input type='text'     name='cost_price[<?= $prod_id ?>]' value='<?= $cost_price ?>' hidden>
...

 

Thank you. Oversight! I later noticed it as well. 

Thank you so much 💓 

Link to comment
Share on other sites

3 hours ago, Barand said:

Remember that only check checkboxes are posted, so the kth checkbox isn't necessarily the kth other items

I'd use the id as index for the post array, for example...

<input type='text'     name='prod_qty[$prod_id]'        value='<?= $prod_qty ?>' hidden>

 

Thank you so very much

Link to comment
Share on other sites

5 minutes ago, I-AM-OBODO said:

No other reason. Its just so the value be available for the form. I don't want the table to tags to have form input inside them cos it will make it not look nice. How would you approach it?

The following values / fields are all "hidden", so they're not really important to the form. I would delete the fields. If you need the "cost_price" information after the form is submitted, you could query the database using the ID passed via "check_list". That way you can be sure the value hasn't been tampered with by the user. It's very easy to go into the code in the browser and modify "hidden" fields. So, someone could modify the price to be 0, for example.

<input type='text'     name='prod_id[<?= $prod_id] ?>'         value='<?= $prod_id ?>' hidden>
<input type='text'     name='cost_price[<?= $prod_id] ?>'      value='<?= $cost_price ?>' hidden>
<input type='text'     name='prod_name[<?= $prod_id] ?>'       value='<?= $prod_name ?>' hidden>
<input type='text'     name='size_type[<?= $prod_id] ?>'       value='<?= $size_type ?>' hidden>
<input type='text'     name='sold_price[<?= $prod_id] ?>'      value='<?= $sold_price ?>' hidden>
<input type='text'     name='prod_qty[<?= $prod_id] ?>'        value='<?= $prod_qty ?>' hidden>
<input type='text'     name='total_price[<?= $prod_id] ?>'     value='<?= $total_price ?>' hidden>

Now, if the form will eventually be modified so that all fields (e.g., cost_price, prod_qty, etc.) can be updated by the user, then it makes sense to have all those fields. Of course, those fields wouldn't be "hidden" in that case.

Link to comment
Share on other sites

6 minutes ago, cyberRobot said:

The following values / fields are all "hidden", so they're not really important to the form. I would delete the fields. If you need the "cost_price" information after the form is submitted, you could query the database using the ID passed via "check_list". That way you can be sure the value hasn't been tampered with by the user. It's very easy to go into the code in the browser and modify "hidden" fields. So, someone could modify the price to be 0, for example.

<input type='text'     name='prod_id[<?= $prod_id] ?>'         value='<?= $prod_id ?>' hidden>
<input type='text'     name='cost_price[<?= $prod_id] ?>'      value='<?= $cost_price ?>' hidden>
<input type='text'     name='prod_name[<?= $prod_id] ?>'       value='<?= $prod_name ?>' hidden>
<input type='text'     name='size_type[<?= $prod_id] ?>'       value='<?= $size_type ?>' hidden>
<input type='text'     name='sold_price[<?= $prod_id] ?>'      value='<?= $sold_price ?>' hidden>
<input type='text'     name='prod_qty[<?= $prod_id] ?>'        value='<?= $prod_qty ?>' hidden>
<input type='text'     name='total_price[<?= $prod_id] ?>'     value='<?= $total_price ?>' hidden>

Now, if the form will eventually be modified so that all fields (e.g., cost_price, prod_qty, etc.) can be updated by the user, then it makes sense to have all those fields. Of course, those fields wouldn't be "hidden" in that case.

What I am trying to say and mean is that I do not want the form input field to show. Even when I used css to make the input field less visible, it still shows. It gives it an aesthetic that I don't like which is why I used table and make the input fields hidden.

Thanks

Link to comment
Share on other sites

18 minutes ago, I-AM-OBODO said:

What I am trying to say and mean is that I do not want the form input field to show.

Deleting the fields wouldn't change the aesthetic. The fields won't show because the are gone.

However, it's starting to sound like I'm missing something. Perhaps you have some JavaScript that unhides the fields so they can be edited. If there's a purpose like that, feel free to ignore the suggestion.

Link to comment
Share on other sites

23 minutes ago, cyberRobot said:

Deleting the fields wouldn't change the aesthetic. The fields won't show because the are gone.

However, it's starting to sound like I'm missing something. Perhaps you have some JavaScript that unhides the fields so they can be edited. If there's a purpose like that, feel free to ignore the suggestion.

I agree with you. Some fields are redundant. I will definitely remove the redundant fields. Thanks

Link to comment
Share on other sites

Sorry for my English, but to save time I had to use GT on some of my postings. Usually, I don't do that. Sometimes it works well, and sometimes it doesn't. I have read thru your code a couple of times, and I think I understand it. If I don't, please put me on the right track again. I am only a human and not a machine. The possibility of being wrong is there, and I'm not very good at this, but I try. I have not read thru the other answers in the thread, so sorry if I step on someone's toes.
Shot in the blind....
From your code, it seems you are using checkboxes to select multiple items for deletion from a table. Right? However, you mentioned that only the first checkbox is working, even when you select the last checkbox.

One issue I see with your code is that you are using the same name attribute for all the checkboxes, which means they will all have the same value when they are submitted. This could be causing the problem you are experiencing.

To fix this issue, you need to give each checkbox a unique name attribute value that corresponds to the product ID for that particular item. You can do this by appending the product ID to the name attribute value of each checkbox using square brackets, like this:

<input type='checkbox' name='check_list[<?= $prod_id ?>]' value='<?= $prod_id ?>' >

In the above code, we are appending the product ID to the name attribute value of the checkbox using PHP. This will ensure that each checkbox has a unique name attribute value that corresponds to the product ID for that particular item.

Then, in your PHP code, you can loop through the $_POST['check_list'] array to get the product IDs of the selected items, like this:

if(!empty($_POST['check_list'])) { foreach($_POST['check_list'] as $prod_id) { // Delete the item with this product ID } }

By doing this, you should be able to select multiple items for deletion using checkboxes and delete them successfully.

  • Like 1
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.