MPDF-在HTML输出中包含PHP变量

时间:2018-11-19 06:21:34

标签: php html mpdf

我如何使用mpdf将php变量替换为html内容?例如下面的代码,我想在html内容中显示变量[HttpPost("AddNewOwner")] public Boolean AddNewOwner(Owner theOwner) { DBConnect objDB = new DBConnect(); string strSQL = "INSERT INTO HomeOwnership_T (HomeOwnerID, FirstName, LastName, Address, City, State, ZipCode, TelNo, Email, BlockNo, LotNo, SaleDate, SalePrice, IsSold) " + "VALUES ('" + theOwner.HomeOwnerID + "', '" + theOwner.FirstName + "', '" + theOwner.LastName + "', '" + theOwner.Address + "', '" + theOwner.City + "', '" + theOwner.State + "', '" + theOwner.ZipCode + "', '" + theOwner.TelNo + "', '" + theOwner.Email + "', '" + theOwner.BlockNo + "', '" + theOwner.LotNo + "', '" + theOwner.SaleDate + "', '" + theOwner.SalePrice + "', '" + theOwner.IsSold + "')"; //Execute the INSERT statement in the database int result = objDB.DoUpdate(strSQL); if (result > 0) { string command = "SELECT TOP 1 HomeOwnerID FROM HomeOwnership_T ORDER BY DESC"; DataSet ds = objDB.GetDataSet(command); Tax taxInfo; foreach (DataRow record in ds.Tables[0].Rows) { taxInfo = new Tax(); taxInfo.HomeOwnerID = int.Parse(record["HomeOwnerID"].ToString()); string strSQL2 = "INSERT INTO TaxInfo_T (AccessedVal, LandVal, AdditionalVal, TaxRate, TaxPerYear, RealEstateTax, HomeOwnerID)" + "VALUES (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '" + taxInfo.HomeOwnerID + "')"; objDB.DoUpdate(strSQL2); } return true; } else { return false; } } [HttpPut] public Boolean Update(Owner theOwner) { DBConnect objDB = new DBConnect(); HomeTax owner = new HomeTax(); string strSQL = "UPDATE HomeOwnership_T SET HomeOwnerID = " + theOwner.HomeOwnerID + ", FirstName = '" + theOwner.FirstName + "', LastName: '" + theOwner.LastName + "', Address = '" + theOwner.Address + "', City = '" + theOwner.City + "', City = '" + theOwner.City + "', State = '" + theOwner.State + "', ZipCode = '" + theOwner.ZipCode + "', TelNo = '" + theOwner.TelNo + "', Email = '" + theOwner.Email + "', BlockNo = " + theOwner.BlockNo + ", LotNo = " + theOwner.LotNo + "', SaleDate = '" + theOwner.SaleDate + "', SalePrice = " + theOwner.SalePrice + ", IsSold = '" + theOwner.IsSold + " WHERE HomeOwnerID = " + theOwner.HomeOwnerID; int result = objDB.DoUpdate(strSQL); if (result > 0) { return true; } else { return false; } } [HttpDelete("{id}")] public Boolean Delete(int id) { DBConnect objDB = new DBConnect(); string strSQL = "DELETE * FROM HomeOwnership_T INNER JOIN TaxInfo_T " + "ON HomeOwnership_T.HomeOwnerID = TaxInfo_T.HomeOwnerID WHERE HomeOwnerID = " + id; int result = objDB.DoUpdate(strSQL); if(result > 0) { return true; } else { return false; } }

$(document).on("click", "#btnAddHomeOwner", function () {

            var strURL = "https://localhost:44395/api/ServiceDeed/AddNewOwner";

            $("#msg").html("");
            $("#update").html("");
            $("#updateResult").html("");

            console.log("btnAddHomeOwner selected");

            var owner = new Object();
            owner.HomeOwnerID = $("#txtHomeOwnerID").val();
            owner.FirstName = $("#txtFirstName").val();
            owner.LastName = $("#txtLastName").val();
            owner.Address = $("#txtAddress").val();
            owner.City = $("#txtCity").val();
            owner.State = $("#txtState").val();
            owner.ZipCode = $("#txtZipCode").val();
            owner.TelNo = $("#txtTelNo").val();
            owner.Email = $("#txtEmail").val();
            owner.BlockNo = $("#txtBlockNo").val();
            owner.LotNo = $("#txtLotNo").val();
            owner.SaleDate = $("#txtSaleDate").val();
            owner.SalePrice = $("#txtSalePrice").val();
            owner.IsSold = $("#txtIsSold").val();

            var strInput = JSON.stringify(owner);

            // Make an AJAX request to get a home and store the response in the appropriate div.
            $.ajax({
                type: "POST",
                url: strURL,
                contentType: "application/json", // set the data type sent to the Web Service.
                dataType: "json",                               // set the data type expected from the Web Service.
                data: strInput,                                 // send an empty JSON object (no input required).
                success: function (data) {                      // set callback function used to update the page/
                    console.log(data);

                    var result = data;

                    if (result == true)
                        $("#msg").text("The record was successfully added to the database.");
                    else
                        $("#msg").text("The record was not added to the database. Try again later.");
                },
                error: function (req, status, error) {          // sets the error callback function used when an error occurs.
                    alert("Error: " + req.responseText + " | " + status + " | " + error);
                }

            }); //end of ajax method
        }); // end of btnStoreHomeOwner click event

        $(document).on("click", "#btnDelete", function () {

            var strURL = "https://localhost:44395/api/ServiceDeed/";

            var param = $("#txtHomeOwnerID").val();

            $("#msg").html("");
            $("#update").html("");
            $("#updateResult").html("");

            console.log("Delete button selected.");

            $.ajax({
                type: "DELETE",
                url: strURL + param,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{5}",
                success: function (data) {
                    console.log(data);

                    var result = data;

                    if (result == true) {
                        $("#msg").text("The record with HomeOwnerID: " + param + " was deleted.");
                        alert("Deleted!");
                    }
                    else {
                        $("#msg").text("The record with HomeOwnerID: " + param + " was not deleted.");
                        alert("Not Deleted");
                    }
                },
                error: function (req, status, error) {          // sets the error callback function used when an error occurs.
                    alert("Error: " + req.responseText + " | " + status + " | " + error);
                }
            }); //end of ajax method
        }); // end of btnDelete click event


$(document).on("click", "#btnUpdateAll", function () {

            var strURL = "https://localhost:44395/api/ServiceDeed/Update";

            // Clear the divs contents.
            //$("#display").html("");
            //$("#msg").html("");
            $("#update").html("");
            $("#updateResult").html("");

            console.log("Update home owner button selected.");

            var owner = new Object();
            owner.HomeOwnerID = $("#txtHomeOwnerID").val();
            owner.FirstName = $("#txtFirstName").val();
            owner.LastName = $("#txtLastName").val();
            owner.Address = $("#txtAddress").val();
            owner.City = $("#txtCity").val();
            owner.State = $("#txtState").val();
            owner.ZipCode = $("#txtZipCode").val();
            owner.TelNo = $("#txtTelNo").val();
            owner.Email = $("#txtEmail").val();
            owner.BlockNo = $("#txtBlockNo").val();
            owner.LotNo = $("#txtLotNo").val();
            owner.SaleDate = $("#txtSaleDate").val();
            owner.SalePrice = $("#txtSalePrice").val();
            owner.IsSold = $("#txtIsSold").val();

            var strInput = JSON.stringify(owner);

            //Make an AJAX request to get a home owner and display the response in the appropriate div
            $.ajax({
                type: "PUT",
                url: strURL,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: strInput,
                success: function (data) {
                    console.log(data);

                    var owner = data;
                    $("#display").html("<hr><p>".concat("HomeOwnerID: ", owner.HomeOwnerID,
                        "<br>FirstName: ", owner.FirstName, "<br>LastName: ", owner.LastName,
                        "<br>Address: ", owner.Address, "<br>City: ", owner.City,
                        "<br>State: ", owner.State, "<br>ZipCode: ", owner.ZipCode,
                        "<br>Telephone Number: ", owner.TelNo, "<br>Email: ", owner.Email,
                        "<br>Block Number: ", owner.BlockNo, "<br>Lot Number: ", owner.LotNo,
                        "<br>Date of Sale: ", owner.SaleDate, "<br>Sale Price: $", owner.SalePrice,
                        "<br>Sold Status: ", owner.IsSold, "<br>Accessed Value: $", owner.AccessedVal, 
                        "<br>Land Value: $", owner.LandVal, "<br>Additional Value: $", owner.AdditionalVal,
                        "<br>Tax Rate: $", owner.TaxRate, "<br>Tax Per Year: $", owner.TaxPerYear,
                        "<br>Real Estate Tax: $", owner.RealEstateTax, "</p >"));

                    if (owner == true)
                        $("#updateResult").text("The record was successfully updated to the database.");
                    else
                        $("#updateResult").text("The record was not updated to the database. Try again later.");
                },
                error: function (req, status, error) {          // sets the error callback function used when an error occurs.
                    alert("Error: " + req.responseText + " | " + status + " | " + error);
                }
            }); //end of ajax method
        }); //end of btnUpdate 

2 个答案:

答案 0 :(得分:0)

<label> variable here :  '.phpvariable.' </label>

断开字符串并将其添加到文本中。

如果您使用",则可以像这样直接使用PHP变量

$html="textual data $varibale_php and rest of the text.";

但是使用'时,可以断开字符串并连接一个值。

答案 1 :(得分:0)

$html = '<html>'.
'<body>'.
'<label> variable here :  '. $phpvariable .' </label>'.
'</body>'.
'</html>';

尝试这样