首页 > 解决方案 > php数组不使用javascript删除行

问题描述

在我修改了数组索引数量以在输入值 = 1 时将减号按钮更改为删除按钮但出现问题并且它没有删除我想隐藏旧按钮进行修改但无法获取新按钮来提交形式。也许这听起来很愚蠢,但作为一名设计师,我尽了最大的努力,所以如果有人能更清楚地看到这一点,请帮助纠正

在此处输入图像描述


PHP部分

<?php
    // REMOVE ITEM
    if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
        // Access the array and run code to remove that array index
        $key_to_remove = $_POST['index_to_remove'];
        if (count($_SESSION["cart_array"]) <= 1) {
            unset($_SESSION["cart_array"]);
        } else {
            unset($_SESSION["cart_array"][$key_to_remove]);
            sort($_SESSION["cart_array"]);
        }
        exit;
    }
    ?>

if ($each_item['quantity'] == 1) {
            $fa_icon = 'fas fa-trash-alt';
            $operation = 'delete_item';
        } else {
            $fa_icon = 'fa fa-minus';
            $operation = 'minusbot';
        }
        

$action = '<button style="width:45px" 
                               type="button" 
            onclick="return update_qty(' . $key_of_session . ',\'' . $operation . '\'),submit()" 
class="btn-subtract btn btn-primary">
                <i class="' . $fa_icon . '"></i>
            </button>';

    $html .= '<form id="form_id_' . $key_of_session . '">
            <div class="input-group-prepend">
                <button style="width:45px" 
                        type="button" 
                        class="btn-add btn btn-primary" 
                        onclick="return update_qty(' . $key_of_session . 
                                 ',\'plusbot\')">
                <i class="fa fa-plus"></i></button>
            </div>
            <input id="kilohere_' . $key_of_session . '" 
                   class="form-control counter" 
                   onchange="return submit_form(' . "'#form_id_" 
                            .$key_of_session ."'". ')" 
                   data-min="1" 
                   max="99" 
                   name="quantity" 
                   type="text" 
                   value="' . $each_item['quantity'] . '" 
                   size="1" 
                   maxlength="2" />
              <div class="input-group-append">' . $action .'</div>
        <input name="item_to_adjust" 
               type="hidden" 
               value="' . $item_id . '" />
        <input name="size" 
               type="hidden" 
               value="' . $size . '" />
        <input name="key_of_session" 
               type="hidden" 
               value="' . $key_of_session . '">
        </form>';

旧按钮的 HTML 部分


// DELETEBUTTON
        $cartOutput .= '
        <td style="border-right:1px solid #0275d8; 
               border-top:1px dotted #BBBBBB ; 
               text-align:center;">
            <form id="form_delete_'.$key_of_session.'">
                <button class="btn btn-primary" type="button"  onclick="return submit_form(' ."'#form_delete_". $key_of_session . "'". ',\'true\');">
                    <i class="fas fa-trash-alt"></i>
                </button>
                <input name="index_to_remove" type="hidden" value="' . $key_of_session . '" />
                <input name="item_id" type="hidden" value="' . $item_id . '" />
            </form>
        </td>';

JAVASCRIPT


// JS

<script>
                function update_qty(id, operation) {
                    let inputField = document.getElementById('kilohere_' + id);
                    let input_value = inputField.value;
                    let post = false;
                    if (operation == 'minusbot' && input_value > 1) {
                        inputField.value = parseInt(inputField.value) - 1;
                        post = true;
                    } else if (operation == 'plusbot' && input_value < 50) {
                        inputField.value = parseInt(inputField.value) + 1;
                        post = true;
                    } else if (operation == 'delete_item') {
                        //DO THE OPERATION HERE
                        var form = document.getElementById("form-id");
                        document.getElementById("your-id").addEventListener("click", function() {
                            form.submit();
                        });
                    }
                    if (post == true) {
                        let form_name = '#form_id_' + id;
                        submit_form(form_name);
                    }
                }

                function submit_form(form_name,delete_item = "false") {
                    let form = document.querySelector(form_name);
                    let form_data = new FormData(form);
                    console.log(form_name)
                    console.log(form_data)
                    var url = 'cart.php';
                    fetch(url, {
                            method: 'POST',
                            body: form_data,
                        })
                        .then(response => response.text()) // response.json() for json data
                        .then(data => {
                            console.log(data)
                            if (delete_item != "false"){
                                window.location.href = window.location;
                            }
                        })
                        .catch((error) => {
                            console.error('Error:', error);
                        });
                }
            </script>

标签: javascriptphphtmlformsforeach

解决方案


推荐阅读