为什么字符串无法从 Javascript 传输到 Php?
Why Did The Strings Fail To Transfer From Javascript To Php?
好的,所以我正在尝试从 javascript 向 php 发送数据,并且我一直在使用整数进行处理。但是,我似乎不能用字符串来做。
在下面的代码中,姓名、电子邮件、公司、phone、城市、州和国家都是字符串字段。其他一切都是一个整数。整数传输得很好,我对变量做了一个警报,我知道它们填充了正确的数据。
(javascript)
已满
"use strict";
const $ = selector => document.querySelector(selector);
const getErrorMsg = lbl =>
`${lbl} must be a valid number greater than zero.`;
const getErrorMsg2 = lbl =>
`${lbl} must be a valid percent less than or equal to 100.`;
const focusAndSelect = selector => {
const elem = $(selector);
elem.focus();
elem.select();
};
const processEntries = () =>
{
let ActiveNumberOfMolds = parseFloat($("#activeNumberOfMolds").value);
let PercentOfDownMolds = parseFloat($("#percentOfDownMolds").value);
PercentOfDownMolds = PercentOfDownMolds * .01;
let AverageLaborHours = parseFloat($("#averageLaborHours").value);
let RepairRatePerHour = parseFloat($("#repairRatePerHour").value);
let CostOfCurrentLifter = parseFloat($("#costOfCurrentLifter").value);
let AverageProfitPerPressHour = parseFloat($("#averageProfitPerPressHour").value);
let EstimatedPriceOfAnAcculifter = parseFloat($("#estimatedPriceOfAnAcculifter").value);
let PercentageReductionInLifterFailureUsingAcculignLifters = parseFloat($("#percentageReductionInLifterFailureUsingAcculignLifters").value);
let AverageNumberOfLiftersPerMold = parseFloat($("#averageNumberOfLiftersPerMold").value);
PercentageReductionInLifterFailureUsingAcculignLifters = PercentageReductionInLifterFailureUsingAcculignLifters * .01;
let LifterCostDifference = (EstimatedPriceOfAnAcculifter - CostOfCurrentLifter);
if(isNaN(ActiveNumberOfMolds) || ActiveNumberOfMolds <= 0){
alert(getErrorMsg("Enter The Active Number Of Molds"));
focusAndSelect("#activeNumberOfMolds");
}
else if (isNaN(AverageNumberOfLiftersPerMold) || AverageNumberOfLiftersPerMold <= 0) {
alert(getErrorMsg("Enter the Average Number Of Lifters Per Mold:"));
focusAndSelect("#averageNumberOfLiftersPerMold");
}
else if (isNaN(PercentOfDownMolds) || PercentOfDownMolds <= 0) {
alert(getErrorMsg("Enter the Percentage Of Down Molds:"));
focusAndSelect("#percentOfDownMolds");
}
else if (isNaN(AverageLaborHours) || AverageLaborHours <= 0) {
alert(getErrorMsg("Enter the Average Labor Hours:"));
focusAndSelect("#averageLaborHours");
}
else if (isNaN(RepairRatePerHour) || RepairRatePerHour <= 0) {
alert(getErrorMsg("Enter the repair rate per hour:"));
focusAndSelect("#repairRatePerHour");
}
else if (isNaN(AverageProfitPerPressHour) || AverageProfitPerPressHour <= 0) {
alert(getErrorMsg("Enter the average profit per press hour:"));
focusAndSelect("#averageProfitPerPressHour");
}
else if (isNaN(CostOfCurrentLifter) || CostOfCurrentLifter <= 0) {
alert(getErrorMsg("Enter the average profit per press hour:"));
focusAndSelect("#costOfCurrentLifter");
}
else if (isNaN(EstimatedPriceOfAnAcculifter) || EstimatedPriceOfAnAcculifter <= 0) {
alert(getErrorMsg("Enter the estimated price of an acculifter:"));
focusAndSelect("#estimatedPriceOfAnAcculifter");
}
else if (PercentageReductionInLifterFailureUsingAcculignLifters <= 0) {
alert(getErrorMsg("Enter the percentage reduction in lifter failure using accualign lifters:"));
focusAndSelect("#percentageReductionInLifterFailureUsingAcculignLifters");
}
else if (PercentOfDownMolds > 1) {
alert(getErrorMsg2("Enter the percentage of down molds:"));
focusAndSelect("#percentOfDownMolds");
}
else if (PercentageReductionInLifterFailureUsingAcculignLifters > 1) {
alert(getErrorMsg2("Enter the Percentage Reduction In Lifter Failure Using Accualign Lifters:"));
focusAndSelect("#percentageReductionInLifterFailureUsingAccualignLifters");
}
else {
$("#MRRPA").value = (ActiveNumberOfMolds * PercentOfDownMolds);
let mrrpa = parseFloat($("#MRRPA").value);
$("#ANHPL").value = (mrrpa * AverageLaborHours);
let anhpl = parseFloat($("#ANHPL").value);
$("#ALCRFLPM").value = ((anhpl * RepairRatePerHour)+(mrrpa*CostOfCurrentLifter*AverageNumberOfLiftersPerMold));
let alcrflpm = parseFloat($("#ALCRFLPM").value);
$("#PLDDM").value = (AverageProfitPerPressHour*anhpl*.3);
let plddm = parseFloat($("#PLDDM").value);
let eacfl = (plddm + alcrflpm);
$("#EACFL").value = (eacfl);
$("#CDBCLVAL").value = (EstimatedPriceOfAnAcculifter-CostOfCurrentLifter);
let pldtd = (PercentageReductionInLifterFailureUsingAcculignLifters*plddm);
let cdbclval = parseFloat($("#CDBCLVAL").value);
$("#TCDBCLVAL").value = (cdbclval*(ActiveNumberOfMolds*AverageNumberOfLiftersPerMold));
let tcdbclval = parseFloat($("#TCDBCLVAL").value);
let acrnm = ((anhpl*RepairRatePerHour*PercentageReductionInLifterFailureUsingAcculignLifters)+(EstimatedPriceOfAnAcculifter*AverageNumberOfLiftersPerMold*ActiveNumberOfMolds*PercentOfDownMolds*PercentageReductionInLifterFailureUsingAcculignLifters));
let cdnlptrc = (tcdbclval+acrnm+pldtd);
let rlfcical = (eacfl-cdnlptrc);
$("#RLFCICAL").value = rlfcical;
$("#EMUUPI").value = ((tcdbclval/rlfcical)*12).toFixed(2);;
let emuupi = parseFloat($("#EMUUPI").value);
console.log("EACFL: " + eacfl );
console.log("cdnlptrc: " + cdnlptrc );
document.getElementById("MRRPA").innerHTML = mrrpa + " Molds";
document.getElementById("ANHPL").innerHTML = anhpl + " Hours";
document.getElementById("ALCRFLPM").innerHTML = "$" + alcrflpm;
document.getElementById("PLDDM").innerHTML = "$" + plddm;
document.getElementById("CDBCLVAL").innerHTML = "$" + cdbclval;
document.getElementById("TCDBCLVAL").innerHTML = "$" + tcdbclval;
document.getElementById("RLFCICAL").innerHTML = "$" + rlfcical;
document.getElementById("EACFL").innerHTML = "$" + eacfl;
document.getElementById("EMUUPI").innerHTML = emuupi + " Months";
document.getElementById("ACRNM").innerHTML = "$" + acrnm;
document.getElementById("PLDTD").innerHTML = "$" + pldtd;
document.getElementById("CDNLPTRC").innerHTML = "$" + cdnlptrc;
if(rlfcical > 0)
{
document.getElementById("RLFCICAL").style.color = "green";
}
else
{
document.getElementById("RLFCICAL").style.color = "red";
}
if(eacfl > 0)
{
document.getElementById("EACFL").style.color = "red";
}
else
{
document.getElementById("EACFL").style.color = "green";
}
if(alcrflpm > 0)
{
document.getElementById("ALCRFLPM").style.color = "red";
}
else
{
document.getElementById("ALCRFLPM").style.color = "green";
}
if(plddm > 0)
{
document.getElementById("PLDDM").style.color = "red";
}
else
{
document.getElementById("PLDDM").style.color = "green";
}
if(tcdbclval > 0)
{
document.getElementById("TCDBCLVAL").style.color = "red";
}
else
{
document.getElementById("TCDBCLVAL").style.color = "green";
}
if(cdbclval)
{
document.getElementById("CDBCLVAL").style.color = "red";
}
else
{
document.getElementById("CDBCLVAL").style.color = "green";
}
if(emuupi > 0)
{
document.getElementById("EMUUPI").style.color = "green";
}
else
{
document.getElementById("EMUUPI").style.color = "red";
}
if(anhpl > 0)
{
document.getElementById("ANHPL").style.color = "red";
}
else
{
document.getElementById("ANHPL").style.color = "green";
}
if(mrrpa > 0)
{
document.getElementById("MRRPA").style.color = "red";
}
else
{
document.getElementById("MRRPA").style.color = "green";
}
if(acrnm > 0)
{
document.getElementById("ACRNM").style.color = "red";
}
else
{
document.getElementById("ACRNM").style.color = "green";
}
if(pldtd > 0)
{
document.getElementById("PLDTD").style.color = "red";
}
else
{
document.getElementById("PLDTD").style.color = "green";
}
if(cdnlptrc > 0)
{
document.getElementById("CDNLPTRC").style.color = "red";
}
else
{
document.getElementById("CDNLPTRC").style.color = "green";
}
let name = document.getElementById("Name").innerHTML;
let email = document.getElementById("Email").innerHTML;
let company = document.getElementById("Company").innerHTML;
let phone = document.getElementById("Phone").innerHTML;
let city = document.getElementById("City").innerHTML;
let state = document.getElementById("State").innerHTML;
let country = document.getElementById("Country").innerHTML;
alert("Test Alert");
alert(name);
alert(email);
alert(company);
alert(phone);
alert(city);
alert(state);
alert(country);
let result = document.querySelector('.result');
// Creating a XHR object
let xhr = new XMLHttpRequest();
let url = "https://staging-dmecompany.kinsta.cloud/Submissions/submitjson.php";
// open a connection
xhr.open("POST", url, true);
// Set the request header i.e. which type of content you are sending
xhr.setRequestHeader("Content-Type", "application/json");
// Create a state change callback
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200)
{
// Print received data from server
document.getElementById("result").innerHTML = this.responseText;
}
};
// Converting JSON data to string
var data = JSON.stringify({ "Name": name, "Email": email, "Company": company, "Phone": phone, "City": city, "State": state, "Country": country, "ActiveNumberOfMolds": ActiveNumberOfMolds, "PercentOfDownMolds": PercentOfDownMolds, "Average Labor Hours": AverageLaborHours, "RepairRatePerHour": RepairRatePerHour, "AverageProfitPerPressHour": AverageProfitPerPressHour, "AverageNumberOfLiftersPerMold": AverageNumberOfLiftersPerMold, "rlfcical": rlfcical, "LifterCostDifference": LifterCostDifference, "anhpl": anhpl, "alcrflpm": alcrflpm, "plddm": plddm, "cdbclval": cdbclval, "pldtd": pldtd, "cdbclval": cdbclval, "emuupi": emuupi, "mrrpa": mrrpa, "acrnm": acrnm, "pldtd": pldtd, "cdnlptrc": cdnlptrc });
alert(data);
// Sending data with the request
xhr.send(data);
}
};
document.addEventListener("DOMContentLoaded", () => {
$("#calculate").addEventListener("click", processEntries);
$("#activeNumberOfMolds").focus();
});
仅包含问题的 TLDR 代码段
let name = document.getElementById("Name").innerHTML;
let email = document.getElementById("Email").innerHTML;
let company = document.getElementById("Company").innerHTML;
let phone = document.getElementById("Phone").innerHTML;
let city = document.getElementById("City").innerHTML;
let state = document.getElementById("State").innerHTML;
let country = document.getElementById("Country").innerHTML;
// Converting JSON data to string
var data = JSON.stringify({ "Name": name, "Email": email, "Company": company, "Phone": phone, "City": city, "State": state, "Country": country, "ActiveNumberOfMolds": ActiveNumberOfMolds, "PercentOfDownMolds": PercentOfDownMolds, "Average Labor Hours": AverageLaborHours, "RepairRatePerHour": RepairRatePerHour, "AverageProfitPerPressHour": AverageProfitPerPressHour, "AverageNumberOfLiftersPerMold": AverageNumberOfLiftersPerMold, "rlfcical": rlfcical, "LifterCostDifference": LifterCostDifference, "anhpl": anhpl, "alcrflpm": alcrflpm, "plddm": plddm, "cdbclval": cdbclval, "pldtd": pldtd, "cdbclval": cdbclval, "emuupi": emuupi, "mrrpa": mrrpa, "acrnm": acrnm, "pldtd": pldtd, "cdnlptrc": cdnlptrc });
// Sending data with the request
xhr.send(data);
所以我将这些东西发送到 php 文档来处理它,但是数据永远不会到达字符串变量。不过 int 变量处理得很好。
(php) 仅包含问题
$data = json_decode(file_get_contents("php://input"));
$myfile = fopen("FilledOut.csv", "a") or die("Unable to open file!");
$txt = "\n $data->name, $data->email, $data->company, $data->phone, $data->city, $data->state, $data->country, $data->ActiveNumberOfMolds, $data->PercentOfDownMolds, $data->RepairRatePerHour, $data->AverageProfitPerPressHour, $data->AverageNumberOfLiftersPerMold, $data->rlfcical, $data->LifterCostDifference, $data->anhpl, $data->alcrflpm, $data->plddm, $data->cdbclval, $data->emuupi, $data->mrrpa, $data->acrnm, $data->pldtd, $data->cdnlptrc, $ipaddress, $date, $time";
fwrite($myfile, $txt);
fclose($myfile);
服务器说字符串变量未定义。
这是服务器说的:
2021/03/02 16:00:32 [error] 54508#54508: *81719 FastCGI sent in
stderr: "PHP message: PHP Notice: Undefined property: stdClass::$name
in /www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP
message: PHP Notice: Undefined property: stdClass::$email in
/www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP
message: PHP Notice: Undefined property: stdClass::$company in
/www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP
message: PHP Notice: Undefined property: stdClass::$phone in
/www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP
message: PHP Notice: Undefined property: stdClass::$city in
/www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP
message: PHP Notice: Undefined property: stdClass::$state in
/www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP
message: PHP Notice: Undefined property: stdClass::$country in
/www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP
message: PHP Warning: fwrite() expects parameter 2 to be string,
object given in /www/dmecompany_192/public/Submissions/submitjson.php
on line 10" while reading response header from upstream, client:
98.243.211.186, server: staging-dmecompany.kinsta.cloud, request: "POST /Submissions/submitjson.php HTTP/1.0", upstream:
"fastcgi://unix:/var/run/php7.3-fpm-dmecompany.sock:", host:
"staging-dmecompany.kinsta.cloud", referrer:
"https://staging-dmecompany.kinsta.cloud/Submissions/AccualignROICalc3.php"
为什么处理字符串失败,而处理整数没有问题?
您的 javascript 对象包含大写属性,而在您的 PHP 代码中您需要小写属性。
将您的 JS 代码编辑为
var data = JSON.stringify({
"name": name,
"email": email,
"company": company,
"phone": phone,
"city": city,
"state": state,
"country": country,
...
});
好的,所以我正在尝试从 javascript 向 php 发送数据,并且我一直在使用整数进行处理。但是,我似乎不能用字符串来做。
在下面的代码中,姓名、电子邮件、公司、phone、城市、州和国家都是字符串字段。其他一切都是一个整数。整数传输得很好,我对变量做了一个警报,我知道它们填充了正确的数据。
(javascript)
已满
"use strict";
const $ = selector => document.querySelector(selector);
const getErrorMsg = lbl =>
`${lbl} must be a valid number greater than zero.`;
const getErrorMsg2 = lbl =>
`${lbl} must be a valid percent less than or equal to 100.`;
const focusAndSelect = selector => {
const elem = $(selector);
elem.focus();
elem.select();
};
const processEntries = () =>
{
let ActiveNumberOfMolds = parseFloat($("#activeNumberOfMolds").value);
let PercentOfDownMolds = parseFloat($("#percentOfDownMolds").value);
PercentOfDownMolds = PercentOfDownMolds * .01;
let AverageLaborHours = parseFloat($("#averageLaborHours").value);
let RepairRatePerHour = parseFloat($("#repairRatePerHour").value);
let CostOfCurrentLifter = parseFloat($("#costOfCurrentLifter").value);
let AverageProfitPerPressHour = parseFloat($("#averageProfitPerPressHour").value);
let EstimatedPriceOfAnAcculifter = parseFloat($("#estimatedPriceOfAnAcculifter").value);
let PercentageReductionInLifterFailureUsingAcculignLifters = parseFloat($("#percentageReductionInLifterFailureUsingAcculignLifters").value);
let AverageNumberOfLiftersPerMold = parseFloat($("#averageNumberOfLiftersPerMold").value);
PercentageReductionInLifterFailureUsingAcculignLifters = PercentageReductionInLifterFailureUsingAcculignLifters * .01;
let LifterCostDifference = (EstimatedPriceOfAnAcculifter - CostOfCurrentLifter);
if(isNaN(ActiveNumberOfMolds) || ActiveNumberOfMolds <= 0){
alert(getErrorMsg("Enter The Active Number Of Molds"));
focusAndSelect("#activeNumberOfMolds");
}
else if (isNaN(AverageNumberOfLiftersPerMold) || AverageNumberOfLiftersPerMold <= 0) {
alert(getErrorMsg("Enter the Average Number Of Lifters Per Mold:"));
focusAndSelect("#averageNumberOfLiftersPerMold");
}
else if (isNaN(PercentOfDownMolds) || PercentOfDownMolds <= 0) {
alert(getErrorMsg("Enter the Percentage Of Down Molds:"));
focusAndSelect("#percentOfDownMolds");
}
else if (isNaN(AverageLaborHours) || AverageLaborHours <= 0) {
alert(getErrorMsg("Enter the Average Labor Hours:"));
focusAndSelect("#averageLaborHours");
}
else if (isNaN(RepairRatePerHour) || RepairRatePerHour <= 0) {
alert(getErrorMsg("Enter the repair rate per hour:"));
focusAndSelect("#repairRatePerHour");
}
else if (isNaN(AverageProfitPerPressHour) || AverageProfitPerPressHour <= 0) {
alert(getErrorMsg("Enter the average profit per press hour:"));
focusAndSelect("#averageProfitPerPressHour");
}
else if (isNaN(CostOfCurrentLifter) || CostOfCurrentLifter <= 0) {
alert(getErrorMsg("Enter the average profit per press hour:"));
focusAndSelect("#costOfCurrentLifter");
}
else if (isNaN(EstimatedPriceOfAnAcculifter) || EstimatedPriceOfAnAcculifter <= 0) {
alert(getErrorMsg("Enter the estimated price of an acculifter:"));
focusAndSelect("#estimatedPriceOfAnAcculifter");
}
else if (PercentageReductionInLifterFailureUsingAcculignLifters <= 0) {
alert(getErrorMsg("Enter the percentage reduction in lifter failure using accualign lifters:"));
focusAndSelect("#percentageReductionInLifterFailureUsingAcculignLifters");
}
else if (PercentOfDownMolds > 1) {
alert(getErrorMsg2("Enter the percentage of down molds:"));
focusAndSelect("#percentOfDownMolds");
}
else if (PercentageReductionInLifterFailureUsingAcculignLifters > 1) {
alert(getErrorMsg2("Enter the Percentage Reduction In Lifter Failure Using Accualign Lifters:"));
focusAndSelect("#percentageReductionInLifterFailureUsingAccualignLifters");
}
else {
$("#MRRPA").value = (ActiveNumberOfMolds * PercentOfDownMolds);
let mrrpa = parseFloat($("#MRRPA").value);
$("#ANHPL").value = (mrrpa * AverageLaborHours);
let anhpl = parseFloat($("#ANHPL").value);
$("#ALCRFLPM").value = ((anhpl * RepairRatePerHour)+(mrrpa*CostOfCurrentLifter*AverageNumberOfLiftersPerMold));
let alcrflpm = parseFloat($("#ALCRFLPM").value);
$("#PLDDM").value = (AverageProfitPerPressHour*anhpl*.3);
let plddm = parseFloat($("#PLDDM").value);
let eacfl = (plddm + alcrflpm);
$("#EACFL").value = (eacfl);
$("#CDBCLVAL").value = (EstimatedPriceOfAnAcculifter-CostOfCurrentLifter);
let pldtd = (PercentageReductionInLifterFailureUsingAcculignLifters*plddm);
let cdbclval = parseFloat($("#CDBCLVAL").value);
$("#TCDBCLVAL").value = (cdbclval*(ActiveNumberOfMolds*AverageNumberOfLiftersPerMold));
let tcdbclval = parseFloat($("#TCDBCLVAL").value);
let acrnm = ((anhpl*RepairRatePerHour*PercentageReductionInLifterFailureUsingAcculignLifters)+(EstimatedPriceOfAnAcculifter*AverageNumberOfLiftersPerMold*ActiveNumberOfMolds*PercentOfDownMolds*PercentageReductionInLifterFailureUsingAcculignLifters));
let cdnlptrc = (tcdbclval+acrnm+pldtd);
let rlfcical = (eacfl-cdnlptrc);
$("#RLFCICAL").value = rlfcical;
$("#EMUUPI").value = ((tcdbclval/rlfcical)*12).toFixed(2);;
let emuupi = parseFloat($("#EMUUPI").value);
console.log("EACFL: " + eacfl );
console.log("cdnlptrc: " + cdnlptrc );
document.getElementById("MRRPA").innerHTML = mrrpa + " Molds";
document.getElementById("ANHPL").innerHTML = anhpl + " Hours";
document.getElementById("ALCRFLPM").innerHTML = "$" + alcrflpm;
document.getElementById("PLDDM").innerHTML = "$" + plddm;
document.getElementById("CDBCLVAL").innerHTML = "$" + cdbclval;
document.getElementById("TCDBCLVAL").innerHTML = "$" + tcdbclval;
document.getElementById("RLFCICAL").innerHTML = "$" + rlfcical;
document.getElementById("EACFL").innerHTML = "$" + eacfl;
document.getElementById("EMUUPI").innerHTML = emuupi + " Months";
document.getElementById("ACRNM").innerHTML = "$" + acrnm;
document.getElementById("PLDTD").innerHTML = "$" + pldtd;
document.getElementById("CDNLPTRC").innerHTML = "$" + cdnlptrc;
if(rlfcical > 0)
{
document.getElementById("RLFCICAL").style.color = "green";
}
else
{
document.getElementById("RLFCICAL").style.color = "red";
}
if(eacfl > 0)
{
document.getElementById("EACFL").style.color = "red";
}
else
{
document.getElementById("EACFL").style.color = "green";
}
if(alcrflpm > 0)
{
document.getElementById("ALCRFLPM").style.color = "red";
}
else
{
document.getElementById("ALCRFLPM").style.color = "green";
}
if(plddm > 0)
{
document.getElementById("PLDDM").style.color = "red";
}
else
{
document.getElementById("PLDDM").style.color = "green";
}
if(tcdbclval > 0)
{
document.getElementById("TCDBCLVAL").style.color = "red";
}
else
{
document.getElementById("TCDBCLVAL").style.color = "green";
}
if(cdbclval)
{
document.getElementById("CDBCLVAL").style.color = "red";
}
else
{
document.getElementById("CDBCLVAL").style.color = "green";
}
if(emuupi > 0)
{
document.getElementById("EMUUPI").style.color = "green";
}
else
{
document.getElementById("EMUUPI").style.color = "red";
}
if(anhpl > 0)
{
document.getElementById("ANHPL").style.color = "red";
}
else
{
document.getElementById("ANHPL").style.color = "green";
}
if(mrrpa > 0)
{
document.getElementById("MRRPA").style.color = "red";
}
else
{
document.getElementById("MRRPA").style.color = "green";
}
if(acrnm > 0)
{
document.getElementById("ACRNM").style.color = "red";
}
else
{
document.getElementById("ACRNM").style.color = "green";
}
if(pldtd > 0)
{
document.getElementById("PLDTD").style.color = "red";
}
else
{
document.getElementById("PLDTD").style.color = "green";
}
if(cdnlptrc > 0)
{
document.getElementById("CDNLPTRC").style.color = "red";
}
else
{
document.getElementById("CDNLPTRC").style.color = "green";
}
let name = document.getElementById("Name").innerHTML;
let email = document.getElementById("Email").innerHTML;
let company = document.getElementById("Company").innerHTML;
let phone = document.getElementById("Phone").innerHTML;
let city = document.getElementById("City").innerHTML;
let state = document.getElementById("State").innerHTML;
let country = document.getElementById("Country").innerHTML;
alert("Test Alert");
alert(name);
alert(email);
alert(company);
alert(phone);
alert(city);
alert(state);
alert(country);
let result = document.querySelector('.result');
// Creating a XHR object
let xhr = new XMLHttpRequest();
let url = "https://staging-dmecompany.kinsta.cloud/Submissions/submitjson.php";
// open a connection
xhr.open("POST", url, true);
// Set the request header i.e. which type of content you are sending
xhr.setRequestHeader("Content-Type", "application/json");
// Create a state change callback
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200)
{
// Print received data from server
document.getElementById("result").innerHTML = this.responseText;
}
};
// Converting JSON data to string
var data = JSON.stringify({ "Name": name, "Email": email, "Company": company, "Phone": phone, "City": city, "State": state, "Country": country, "ActiveNumberOfMolds": ActiveNumberOfMolds, "PercentOfDownMolds": PercentOfDownMolds, "Average Labor Hours": AverageLaborHours, "RepairRatePerHour": RepairRatePerHour, "AverageProfitPerPressHour": AverageProfitPerPressHour, "AverageNumberOfLiftersPerMold": AverageNumberOfLiftersPerMold, "rlfcical": rlfcical, "LifterCostDifference": LifterCostDifference, "anhpl": anhpl, "alcrflpm": alcrflpm, "plddm": plddm, "cdbclval": cdbclval, "pldtd": pldtd, "cdbclval": cdbclval, "emuupi": emuupi, "mrrpa": mrrpa, "acrnm": acrnm, "pldtd": pldtd, "cdnlptrc": cdnlptrc });
alert(data);
// Sending data with the request
xhr.send(data);
}
};
document.addEventListener("DOMContentLoaded", () => {
$("#calculate").addEventListener("click", processEntries);
$("#activeNumberOfMolds").focus();
});
仅包含问题的 TLDR 代码段
let name = document.getElementById("Name").innerHTML;
let email = document.getElementById("Email").innerHTML;
let company = document.getElementById("Company").innerHTML;
let phone = document.getElementById("Phone").innerHTML;
let city = document.getElementById("City").innerHTML;
let state = document.getElementById("State").innerHTML;
let country = document.getElementById("Country").innerHTML;
// Converting JSON data to string
var data = JSON.stringify({ "Name": name, "Email": email, "Company": company, "Phone": phone, "City": city, "State": state, "Country": country, "ActiveNumberOfMolds": ActiveNumberOfMolds, "PercentOfDownMolds": PercentOfDownMolds, "Average Labor Hours": AverageLaborHours, "RepairRatePerHour": RepairRatePerHour, "AverageProfitPerPressHour": AverageProfitPerPressHour, "AverageNumberOfLiftersPerMold": AverageNumberOfLiftersPerMold, "rlfcical": rlfcical, "LifterCostDifference": LifterCostDifference, "anhpl": anhpl, "alcrflpm": alcrflpm, "plddm": plddm, "cdbclval": cdbclval, "pldtd": pldtd, "cdbclval": cdbclval, "emuupi": emuupi, "mrrpa": mrrpa, "acrnm": acrnm, "pldtd": pldtd, "cdnlptrc": cdnlptrc });
// Sending data with the request
xhr.send(data);
所以我将这些东西发送到 php 文档来处理它,但是数据永远不会到达字符串变量。不过 int 变量处理得很好。
(php) 仅包含问题
$data = json_decode(file_get_contents("php://input"));
$myfile = fopen("FilledOut.csv", "a") or die("Unable to open file!");
$txt = "\n $data->name, $data->email, $data->company, $data->phone, $data->city, $data->state, $data->country, $data->ActiveNumberOfMolds, $data->PercentOfDownMolds, $data->RepairRatePerHour, $data->AverageProfitPerPressHour, $data->AverageNumberOfLiftersPerMold, $data->rlfcical, $data->LifterCostDifference, $data->anhpl, $data->alcrflpm, $data->plddm, $data->cdbclval, $data->emuupi, $data->mrrpa, $data->acrnm, $data->pldtd, $data->cdnlptrc, $ipaddress, $date, $time";
fwrite($myfile, $txt);
fclose($myfile);
服务器说字符串变量未定义。
这是服务器说的:
2021/03/02 16:00:32 [error] 54508#54508: *81719 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined property: stdClass::$name in /www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP message: PHP Notice: Undefined property: stdClass::$email in /www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP message: PHP Notice: Undefined property: stdClass::$company in /www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP message: PHP Notice: Undefined property: stdClass::$phone in /www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP message: PHP Notice: Undefined property: stdClass::$city in /www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP message: PHP Notice: Undefined property: stdClass::$state in /www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP message: PHP Notice: Undefined property: stdClass::$country in /www/dmecompany_192/public/Submissions/submitjson.php on line 9PHP message: PHP Warning: fwrite() expects parameter 2 to be string, object given in /www/dmecompany_192/public/Submissions/submitjson.php on line 10" while reading response header from upstream, client: 98.243.211.186, server: staging-dmecompany.kinsta.cloud, request: "POST /Submissions/submitjson.php HTTP/1.0", upstream: "fastcgi://unix:/var/run/php7.3-fpm-dmecompany.sock:", host: "staging-dmecompany.kinsta.cloud", referrer: "https://staging-dmecompany.kinsta.cloud/Submissions/AccualignROICalc3.php"
为什么处理字符串失败,而处理整数没有问题?
您的 javascript 对象包含大写属性,而在您的 PHP 代码中您需要小写属性。
将您的 JS 代码编辑为
var data = JSON.stringify({
"name": name,
"email": email,
"company": company,
"phone": phone,
"city": city,
"state": state,
"country": country,
...
});