【问题标题】:Two submit buttons with same action and one from them with added function/action for the same form两个具有相同操作的提交按钮,其中一个为同一表单添加了功能/操作
【发布时间】:2016-11-29 11:37:35
【问题描述】:

我有一个 form 和两个 submit 按钮。

<form id="manageSalesForm" name="manageSalesForm" method="post" action="<?php echo BASE_URL?>includes/functions/sales_functions.php">

PROCEED 按钮应该将数据提交到数据库(这工作)

<input type="submit" name="btnProceed" id="btnProceed" value="PROCEED" onclick="document.getElementById('txtSubTotal').value = '';"/>

PRINT &amp; PROCEED按钮应该提交数据到数据库并打印页面(怎么做?)

<input type="submit" name="btnPrintReceipt" id="btnPrintReceipt" value="PRINT &amp; PROCEED" formaction="<?php echo BASE_URL?>reports/salesreceipt2.php" formtarget="_blank"/>

salesreceipt2.phpfpdf 代码,应该在新标签/窗口中打开它。

同一个表单有另一个button类型的按钮

<button type="button" name="btnSave" id="btnSave" onclick="submitdata(); resetform();">ADD</button>

function submitdata() {
              var listItemName  = document.getElementById("listItemName").value;
              var listStock = document.getElementById("listStock").value;
              var txtUnitPrice = document.getElementById("txtUnitPrice").value;
              var txtQuantity = document.getElementById("txtQuantity").value;
              var listCustomer = document.getElementById("listCustomer").value;
              var txtReceiptNo = document.getElementById("txtReceiptNo").value;
              var TheDate = document.getElementById("TheDate").value;

              // Returns successful data submission message when the entered information is stored in database.
              var dataString = {listItemName:listItemName, listStock: listStock, txtUnitPrice: txtUnitPrice, txtQuantity: txtQuantity, listCustomer: listCustomer, txtReceiptNo: txtReceiptNo};
              if (listItemName == '' || listStock == ''|| txtUnitPrice == ''|| txtQuantity == ''|| listCustomer == ''|| txtReceiptNo == ''|| TheDate == '') {
              salesitemsAddFail();
              } 
              else {
                         // AJAX code to submit form.
                         $.ajax({
                         type: "POST",
                         url: "/pms/includes/functions/sales_temp_functions.php",
                         data: dataString,
                         cache: false,
                         success: function(html) {    

              //reload the sales datagrid once add the item details to temporary table (sales_temp)
              $('#list').trigger("reloadGrid",[{page:1}]);
                 //window.location.reload();
                 //refresh/update the sub total value when adding
                 $("#sub_total_div").load(location.href + " #sub_total_div");

                         }
                         });
                     }
         }

我尝试了几种方法,但无法成功。感谢您的帮助。

【问题讨论】:

  • 试试&lt;a&gt;&lt;/a&gt;而不是typ='submit'
  • 你用 AJAX 插入记录吗?
  • @ErolKESKİN 是的,添加了代码。

标签: javascript php jquery html forms


【解决方案1】:

首先更改所有提交到按钮。像这样:

<input type="button" value="Proceed" onclick="proceed(false)" />
<input type="button" value="Proceed & Print" onclick="proceed(true)" />

现在添加这个 Javascript:

function print(recordId){
    window.open( 'baseurl/salesreceipt2.php?id='+recordId , '_blank');
}
function proceed(printIt){
    // your ajax operations..
    $.ajax({
        //your ajax configs..
        success:function(response){
            //your ajax success things..
            if(printIt == true){
                print(response.lastId); // Pass last Id of record to print function if your salesreceipt2.php always prints last record that's unnecessary.
            }
        }
    });
}

这很容易。如果 printIt (您的第一个参数)为真,它会使用 LastRecordId 调用 print(如果您的 salesreceipt2.php 总是打印最后一条记录,正如我所说的那样,它是不必要的。)如果不是,您应该返回一个包含插入 ID 的 JSON 响应。所以这个插入的id会通过?id传递给salesreceipt2.php文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2016-08-21
    相关资源
    最近更新 更多