在不使用提交按钮的情况下检测 PHP 表单的任何更改

我需要帮助。现在我尝试创建一个可以检测 PHP 表单中的任何更改值的函数。现在我正在使用 CodeIgniter。我对此进行了更详细的解释。


我做一个电子商务网站。如果用户在购物车页面中并更改了数量但用户尚未结帐并且仍想浏览更多......当用户必须更改数量并按到另一个页面时。它将显示弹出警报。此弹出警报具有保存更改数量的功能。但是如果用户没有改变任何东西。不应显示弹出警报。


我已经在菜单中设置了这个。


<?php echo form_open('order_products_execute', 'class="order_form"'); ?>

                <?php include(VIEWPATH.'_order_parts.html') ?>

                <div class="common_btn_area">

                    <input type="hidden" name="branch_id" value="<?php echo $branch_id; ?>">

                    <button type="button" class="add_cart_more">add more</button>

                    <button type="submit" class="common_save_btn confirmation">Order</button>

                </div>

<?php echo form_close(); ?>


<!-- footer menu -->

<ul>

    <li>

       <a href="<?php echo base_url('top/'); ?>" class="footer_link" onclick="ExitCart('<?php echo base_url('top/'); ?>')" >

         <span>Home</span>

       </a>

    </li>

    <li>

       <a href="<?php echo base_url('product/'); ?>" class="footer_link" onclick="ExitCart('<?php echo base_url('product/'); ?>')" >

         <span>Product</span>

       </a>

    </li>

</ul>

并设置脚本


<script>

function ExitCart(link){

            var $form = $('.order_form');

                                    }

                                }

                            });

                        }else{

                            #if confirm not

                            location.href = link;

                        };

                    }else{

                        return true;

                    }

                }

            });

}

</script>

当我实现这个 ajax 代码时,有时代码可以工作。但有时并非如此。是否有任何代码可以检测表单的任何更改。但没有按下提交按钮。


绝地无双
浏览 177回答 2
2回答

幕布斯6054654

在 jQuery 中,您可以绑定所有输入更改$('.order_form input').change(functon(){var $form = $('.order_form');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #code for compare previous value with changing value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax( {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: $form.attr('method'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : "/buyer/ajax/compare_form_add_cart",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType : "json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data : $form.serialize(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success : function(resultdata) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(resultdata){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(confirm("Do you want to save your changes?")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #if confirm yes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'post',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '/buyer/Ajax/add_order_data_in_cart_session',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: $('.order_form').serialize(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(res, textStatus, xhr){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(res.result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; location.href = link;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( "#loading_layer" ).css('display', 'none');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('Failed to save cart data. Please try again.');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #if confirm not&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; location.href = link;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });})

呼啦一阵风

例如,您需要复制输入字段// Input Field (TEXT)< input type="text" id="input1" value="Same Value" />// Hidden Input for comparison< input type="hidden" value="Same Value" />在点击功能上,您应该比较这些字段,例如function ExitCart(link){&nbsp; &nbsp; &nbsp;// Get Input Value&nbsp; &nbsp; &nbsp;var val = $.trim($('#input1').val());&nbsp; &nbsp; // Get Reference Value from next input&nbsp; &nbsp; var valChk = $.trim($('#input1').next().val());&nbsp; &nbsp; if(val != valChk) {&nbsp; &nbsp; &nbsp; &nbsp; YOUR CODE HERE&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP