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条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题