打印机脚本运行后,Javascript自动提交未执行

问题描述 投票:0回答:2

我是一个javascript新手。我有一个名为sendToQuickPrinter()的打印机功能,它打印正常,但当该脚本完成后,我需要自动提交表单以返回到我的“cart.php”页面。我想我真的很亲近。请帮忙。

<script>

function sendToQuickPrinter(){

    var text =
    "<CENTER><MEDIUM2><?php echo $shop_name ?><BR>" +
    "<CENTER><?php echo $display_date ?>        <?php echo $display_time ?><BR><BR>" +
    "Description          Price<BR>" +
    "<?php $i=0;while($i < $print_count){$i=$i+1; echo $p_name_array[$i].';; ;;'.$transaction_qty_array[$i].' @ '.number_format($p_price_array[$i],2).'<BR>';} ?>" +
    "<BR>" +
    "<CENTER>Sub-total      $<?php echo number_format($subtotal,2) ?><BR>" +
    "<CENTER>Tax            $<?php echo number_format($subtax,2) ?><BR>" +
    "<CENTER>Total          $<?php echo number_format($item_total,2) ?><BR>" +
    "<BR>" +
    "<CENTER>Thank you for shopping with us, we appreciate your business!<BR>" +
    "<CENTER><MEDIUM2> Have a great day!<BR>" +
    "<BR>" +
    "<CUT>" +
    "DRAWER";
        var textEncoded = encodeURI(text);
        window.location.href="quickprinter://"+textEncoded;

        document.frm2.submit()  // !!!important- I auto submit frm2 below
}

sendToQuickPrinter();

</script>

<form  action="cart.php" name="frm2" method="post">
    <input type="hidden" name="longitude" id="getlon" />
    <input type="hidden" name="latitude" id="getlat" />
    <input type="hidden" name="shop_name" value="<?php echo $shop_name ?>" />
</form>
javascript php html
2个回答
0
投票

document.frm2.submit()改变document.forms["frm2"].submit()

function sendToQuickPrinter(){

  var text =
  "<CENTER><MEDIUM2><?php echo $shop_name ?><BR>" +
  "<CENTER><?php echo $display_date ?>        <?php echo $display_time ?><BR><BR>" +
  "Description          Price<BR>" +
  "<?php $i=0;while($i < $print_count){$i=$i+1; echo $p_name_array[$i].';; ;;'.$transaction_qty_array[$i].' @ '.number_format($p_price_array[$i],2).'<BR>';} ?>" +
  "<BR>" +
  "<CENTER>Sub-total      $<?php echo number_format($subtotal,2) ?><BR>" +
  "<CENTER>Tax            $<?php echo number_format($subtax,2) ?><BR>" +
  "<CENTER>Total          $<?php echo number_format($item_total,2) ?><BR>" +
  "<BR>" +
  "<CENTER>Thank you for shopping with us, we appreciate your business!<BR>" +
  "<CENTER><MEDIUM2> Have a great day!<BR>" +
  "<BR>" +
  "<CUT>" +
  "DRAWER";
    var textEncoded = encodeURI(text);
    window.location.href="quickprinter://"+textEncoded;

    document.forms["frm2"].submit()  // !!!important- I auto submit frm2 below
}

0
投票

将您的脚本移动到</body>标记之前或form之下它将起作用。

    
    function sendToQuickPrinter(){
    
        var text =
        "<CENTER><MEDIUM2><?php echo $shop_name ?><BR>" +
        "<CENTER><?php echo $display_date ?>        <?php echo $display_time ?><BR><BR>" +
        "Description          Price<BR>" +
        "<?php $i=0;while($i < $print_count){$i=$i+1; echo $p_name_array[$i].';; ;;'.$transaction_qty_array[$i].' @ '.number_format($p_price_array[$i],2).'<BR>';} ?>" +
        "<BR>" +
        "<CENTER>Sub-total      $<?php echo number_format($subtotal,2) ?><BR>" +
        "<CENTER>Tax            $<?php echo number_format($subtax,2) ?><BR>" +
        "<CENTER>Total          $<?php echo number_format($item_total,2) ?><BR>" +
        "<BR>" +
        "<CENTER>Thank you for shopping with us, we appreciate your business!<BR>" +
        "<CENTER><MEDIUM2> Have a great day!<BR>" +
        "<BR>" +
        "<CUT>" +
        "DRAWER";
            var textEncoded = encodeURI(text);
            window.location.href="quickprinter://"+textEncoded;
    
            document.frm2.submit()  // !!!important- I auto submit frm2 below
    }
    
    //sendToQuickPrinter();
<form  action="cart.php" name="frm2" method="post">
        <input type="hidden" name="longitude" id="getlon" />
        <input type="hidden" name="latitude" id="getlat" />
        <input type="hidden" name="shop_name" value="<?php echo $shop_name ?>" />
    </form>
    
    <button id="print" click="sendToQuickPrinter();">Print</button>
© www.soinside.com 2019 - 2024. All rights reserved.