dongwuying0221 2019-01-06 20:31 采纳率: 0%
浏览 281
已采纳

使用JS提交表单并仍然成功发布所有表单数据的正确方法是什么?

I'm working with an embedded app on our dev site and when I click the submit button inside the iframe, I am triggering a manual submission event on another form (not in an iframe) on that page. If I manually click the submit button for the form, my data posts and everything works correctly. However, I want to eliminate an extra user click and submit the external form automatically when a user submits the other form inside the iframe.

I've got everything working correctly on a base level. When a user clicks the submit button in the iframe, I am using JQuery to grab values from inside the iframe and set values in this external form. Using the jquery 'submit()' event, I am then able to submit that external form. The problem is, the page refreshes and the data doesn't go anywhere. If I remove the 'submit()' event and manually click the submit button, the form posts and in this case, adds a product with custom data to the product cart.

As a proof of concept, this is my 'iframed' HTML.

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <h1>Proof of Concept</h1>
        <p>Total cost: $<span id="cust_price">222.22</span> plus shipping.</p>
        <p>Quote number: <span id="quot_num">1546751962211</p>

        <form method="POST" enctype="multipart/form-data" id="newQuoteForm">
            <button type="submit" class="btn btn-primary" name="new-app-btn">Add to Cart</button>
        </form>
    </body>
    <footer>
    </footer>
</html>

Here is my on-page form that is OUTSIDE the iFrame.

<form method="POST" enctype="multipart/form-data" id="outer-quote-form" action="/checkout/">
    <label class="quote_number">Quote Number: 
        <input type="text" id="quote_number" name="quote_number" value="">
    </label>
    <label class="custom_price">price:
        <input type="text" id="custom_price" name="custom_price" value="">
    </label>
    <button type="submit" class="btn btn-primary" name="ws-add-to-cart">Add to Cart</button>
</form>

Then, I have JQuery working to grab the iframed values and puts them in the exterior form. Afterwards, it fires a 'submit()' event on that form.

<script>    
    jQuery('#newQuoteApp').load(function() {
        var iFrameDOM = jQuery("iframe#newQuoteApp").contents();
        jQuery('#newQuoteApp').contents().find('#newQuoteForm').submit(function() { 
            jQuery("input#custom_price").val(jQuery('#newQuoteApp').contents().find('#cust_price').text()); // updated
            jQuery("input#quote_number").val(jQuery('#newQuoteApp').contents().find('#quot_num').text());

            jQuery("#outer-quote-form").submit();
            return true; //return false prevents submit
        });
    }); 
</script>

Except when the jquery submit() event fires, the form appears to submit and the page refreshes but no data is posting as it does when I manually submit the form. Is there an extra step here or a better way to fire the form submit with post data?

Edit: Adding the PHP function that isn't firing on jquery submit() for context.

if (isset($_POST['ws-add-to-cart'])) {
    add_action( 'init', 'add_product_to_cart' );
    function add_product_to_cart() {
        global $woocommerce;
        global $product;
        $product_id = 138;
        $woocommerce->cart->add_to_cart($product_id);
    }
    header("Location:https://www.devsite.com/checkout/");
}
  • 写回答

2条回答 默认 最新

  • duanqiao3608 2019-01-07 09:47
    关注

    The reason for the form not submitting because you are submitting the whole form without the submit button which is
    <button type="submit" class="btn btn-primary" name="ws-add-to-cart">Add to Cart</button>
    which you have declared in php to get a post request like this

    if (isset($_POST['ws-add-to-cart'])) {...
    

    When you call submit(); on the form via the get method, you see
    '/new-quote/?quote_number=1546751962211&custom_price=222.22'
    but where's ws-add-to-cart, it's not submitting and that's the reason why php isn't getting your request

    The fix will be to add .click() on the submit button instead of submitting the form

    <script>
      function enterVals($val){
        var price = $val.price;
        document.getElementById("quote_number").value = $val.num
        document.getElementById("custom_price").value = $val.price
        document.getElementsByName("ws-add-to-cart").click();
      }
    </script>
    

    Or in your script in case you want to use jquery, this is the fix

    <script>    
        jQuery('#newQuoteApp').load(function() {
            var iFrameDOM = jQuery("iframe#newQuoteApp").contents();
            jQuery('#newQuoteApp').contents().find('#newQuoteForm').submit(function() { 
                jQuery("input#custom_price").val(jQuery('#newQuoteApp').contents().find('#cust_price').text()); // updated
                jQuery("input#quote_number").val(jQuery('#newQuoteApp').contents().find('#quot_num').text());
    
                jQuery("button[name=ws-add-to-cart]").click();
                return true; //return false prevents submit
            });
        }); 
    </script>
    

    This is definitely the answer and sorry for my stupidity, i didn't pay required attention before

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移
  • ¥15 如何用python处理excel的数据(极值标准化)