0

I have a web app that is basically a huge form that will eventually be transformed into a pdf. When the form is submitted, all the input gets sent to a php file that stores them as variables to be added to the pdf. Some of the data I need on the pdf is calculated in JavaScript, so I am using ajax to send those calculated variables to the same php file that the form sends its data to. The ajax call is not sending the data to the php script. I tried to echo some of the variables, but nothing shows up. Here is my ajax call:

$.ajax({
        url: 'FMpdf_values.php',
        method: 'POST',
        data: {elevChartImg : elevChartImg, azChartImg : azChartImg, power_rating : power_rating, antWeight : antWeight, antennaWithRadomeWeight : antWithRadomeWeight,
               epa : epa, radome : radome, deicer : deicer, thetaCol : thetaCol, afAmpCol : afAmpCol, dbCol : dbCol, product : product, channelFM : channelFM,
               antLongDescription : antLongDescription, antShortDescription : antShortDescription},
        success:function(data){
            console.log(data);
        }
    });

The PHP file is just setting the variables to $_POST['some_data'], then that file is included in the PHP file that creates the pdf. I am relatively new to both PHP and ajax, so I apologize if this is an easy fix. I am wondering if it is even possible to get values from the HTML form and ajax in the same file?

FMpdf_values.php:

<?php

//PHP file for getting the values needed for the PDF
//All html element variables are added here, while calculated values are passed through AJAX

header("Content-Type: application/json", true); //For accepting JSON data

//HTML Element Variables
$projectName = $_POST['projectTitle'];
$customer = $_POST['customer'];
$location = $_POST['location'];
$antModel = $_POST['tlantennaModel'];
$specCode = $_POST['specNumber'];
$createdBy = $_POST['completedBy'];
$notes = $_POST['notes'];
$date = date("F j, Y");
$revDate = $_POST['revisionDate'];
$freq = $_POST['channelNum'];
$wavelength = "11807.22891";
$gainVal = $_POST['tlhorPeakPower'];
$erpVal = $_POST['tlhorERP'];
$beamtilt = $_POST['beamTiltVal'];
$firstNull = $_POST['firstNull'];
$secondNull = $_POST['secondNull'];
$rfInput = $_POST['rfInputs'];

$antennaHeightFt = $_POST['antennaHeightFt'];
$antennaHeightMt = $_POST['antennaHeightMt'];
$CORHeightFt = $_POST['CORHeightFt'];
$CORHeightMt = $_POST['CORHeightMt'];
$ovrHeightFt = $_POST['overallHeightFt'];
$ovrHeightMt = $_POST['overallHeightMt'];

$vertLineType = $_POST['tlTypeVertSelect'];
$vertLineLengthFt = $_POST['tlvertft'];
$vertLineLengthMt = $_POST['vertLengthMeters'];
$vertAttenFt = $_POST['tlvertatten'];
$vertAttenMt = $_POST['tlVertAttenMt']; 

$horLineType = $_POST['tlTypeHorSelect'];
$horLineLengthFt = $_POST['tlhorft'];
$horLineLengthMt = $_POST['horLengthMeters'];
$horAttenFt = $_POST['tlhoratten'];
$horAttenMt = $_POST['tlhorattenmt'];

$transLineLosskW = $_POST['tranlinelossKW'];
$transLineLossdB = $_POST['tranlinelossdb'];
$transLineEff = $_POST['tlefficiency'];
$antInputPowerkW = $_POST['antennaInputPowerkw'];
$antInputPowerdB = $_POST['antennaInputPowerdBk'];

$isoTranLosskW = $_POST['isoLosskw'];
$isoTranLossdB = $_POST['isoLossdB'];
$filterLosskW = $_POST['filterLosskw'];
$filterLossdB = $_POST['filterLossdB'];

$systemLosskW = $_POST['systemLosskw'];
$systemLossdB = $_POST['systemLossdB'];
$totalLosskW = $_POST['totallosskw'];
$totalLossdB = $_POST['totallossdB'];
$TPOkW = $_POST['poweroutputkw'];
$TPOdB = $_POST['poweroutputdB'];
$systemEff = $_POST['otherSystemEfficiency'];


//Variables from AJAX
$elevChartImg = $_POST['elevChartImg'];
$azChartImg = $_POST['azChartImg'];
$power_rating = $_POST['power_rating'];
$antWeight = $_POST['antWeight'];
$antWithRadomeWeight = $_POST['antWithRadomeWeight'];
$epa = $_POST['epa'];
$radome = $_POST['radome'];
$deicer = $_POST['deicer'];
$thetaCol = $_POST['thetaCol'];
$afAmpCol = $_POST['afAmpCol'];
$dbCol = $_POST['dbCol'];
$product = $_POST['product'];
$channelFM = $_POST['channelFM'];
$antLongDescription = $_POST['antLongDescription'];
$antShortDescription = $_POST['antShortDescription'];


//ThetaCol, afAmpCol, & dbCol are currently JSON encoded objects
$thetaCol = json_decode($thetaCol);
$afAmpCol = json_decode($afAmpCol);
$dbCol = json_decode($dbCol);

die(print_r($_POST));
8
  • 2
    What do you see when you put this in the receiving PHP script: die(print_r($_POST));? Commented Sep 23, 2022 at 17:50
  • 2
    FYI, {var1: var1, var2: var2, ...} can be abbreviated to {var1, var2, ...}. Commented Sep 23, 2022 at 17:55
  • 2
    Please provide a minimal reproducible example. Maybe the problem is with how you're setting the variables. Commented Sep 23, 2022 at 17:56
  • 2
    You can use $("form").serialize() to get all the fields of the form. Commented Sep 23, 2022 at 17:56
  • 2
    Also, what does console.log(data); show? How, where are you setting the data variables: elevChartImg, azChartImg etc.... How are you calling $.ajax()? Commented Sep 23, 2022 at 17:56

1 Answer 1

1

You are basically calling your PHP script 2 times.

At the first time you are firing up an ajax call (Your php script executes at the server side and PDF generator code runs and generates invalid PDF, as you don't have the form data yet)

Second time the actual form is submitted, your PHP script executes again, this time you don't have the AJAX data, so again the target PDF will be invalid)

Instead of sending data in 2 parts (one part using form POST and another part using AJAX POST), have some hidden fields in your form, fire the javascript calculation function on click of your submit button, fill up the hidden fields and then submit the form. This way you get all the data at one go.

Let me know if that make sense and if you are able to proceed ahead or do you need some sort of code samples..

Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense, I think that is the route I need to go. Thank you!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.