Skip to content
Advertisement

Why Did The Strings Fail To Transfer From Javascript To Php? [closed]

Ok, so I am trying to send data to php from javascript and I’ve been doing it just fine with ints. However, I can’t seem to do it with strings.

On the below code, the name, email, company, phone, city, state and country are all string fields. Everything else is an int. The ints are transfering just fine, and I’ve done an alert on the variables and I know that they are populated with the right data.

(javascript)

Full

  "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 Snippet that contains just the problem

 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); 

So I send the stuff off to the php document to process it, but the data is never arriving to the string variables. The int variables process just fine though.

(php) Contains just the problem

$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);

The server said that the variables that are strings are undefined.

Here is what the server said:

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”

Why are the strings failing to process, while the integers are processed without an issue?

Advertisement

Answer

Your javascript object contains capitalized properties, while in your PHP code you are expecting lowercased properties.

Edit your JS code to

var data = JSON.stringify({
  "name": name,
  "email": email,
  "company": company,
  "phone": phone,
  "city": city,
  "state": state,
  "country": country,
  ...
});
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement