如何解决为什么字符串无法从 Javascript 传输到 PHP?
好的,所以我正在尝试从 javascript 向 PHP 发送数据,我一直在用 int 做的很好。但是,我似乎无法用字符串来做到这一点。
在下面的代码中,姓名、电子邮件、公司、电话、城市、州和国家都是字符串字段。其他一切都是int。整数传输得很好,我已经对变量发出警报,我知道它们填充了正确的数据。
(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,"emuupi": emuupi,"mrrpa": mrrpa,"acrnm": acrnm,"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,"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 [错误] 54508#54508:*81719 FastCGI 发送 stderr: "PHP 消息:PHP 通知:未定义的属性:stdClass::$name 在第 9PHP 行的 /www/dmecompany_192/public/Submissions/submitjson.PHP 消息:PHP 注意:未定义的属性:stdClass::$email in /www/dmecompany_192/public/Submissions/submitjson.PHP 第 9PHP 行 消息:PHP 注意:未定义的属性:stdClass::$company in /www/dmecompany_192/public/Submissions/submitjson.PHP 第 9PHP 行 消息:PHP 注意:未定义的属性:stdClass::$phone in /www/dmecompany_192/public/Submissions/submitjson.PHP 第 9PHP 行 消息:PHP 注意:未定义的属性:stdClass::$city in /www/dmecompany_192/public/Submissions/submitjson.PHP 第 9PHP 行 消息:PHP 注意:未定义的属性:stdClass::$state in /www/dmecompany_192/public/Submissions/submitjson.PHP 第 9PHP 行 消息:PHP 注意:未定义的属性:stdClass::$country in /www/dmecompany_192/public/Submissions/submitjson.PHP 第 9PHP 行 消息:PHP 警告:fwrite() 期望参数 2 为字符串, /www/dmecompany_192/public/Submissions/submitjson.PHP 中给出的对象 在第 10 行”从上游读取响应头时,客户端: 98.243.211.186,服务器:staging-dmecompany.kinsta.cloud,请求:“POST /Submissions/submitjson.PHP HTTP/1.0”,上游: “fastcgi://unix:/var/run/PHP7.3-fpm-dmecompany.sock:”,主机: “staging-dmecompany.kinsta.cloud”,推荐人: "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,...
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。