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

时间:2019-04-26 06:21:11

标签: javascript php html

我是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>

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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>