-3

Why Are My Variables Coming Back Us Undefined From External Javascript Page?

So basically, I am sending variables to javascript from a php page. On page 1, the users fills out a form.

The data is then passed to page 2, where the data is taken from the post and is put into php variables then echoed onto the page.

    $name = isset($_POST['first_name']) ? $_POST['first_name'] : "";
    $email = isset($_POST['email']) ? $_POST['email'] : "";
    $company = isset($_POST['company']) ? $_POST['company'] : "";
    $phone = isset($_POST['phone']) ? $_POST['phone'] : "";
    $city = isset($_POST['city']) ? $_POST['city'] : "";
    $state = isset($_POST['state']) ? $_POST['state'] : "";
    $country = isset($_POST['country']) ? $_POST['country'] : "";
    <p id="Name"><?php echo $name ?></p>
    <p id="Email"><?php echo $email ?></p>
    <p id="Company"><?php echo $company ?></p>
    <p id="Phone"><?php echo $phone ?></p>
    <p id="City"><?php echo $city ?></p>
    <p id="State"><?php echo $state ?></p>
    <p id="Country"><?php echo $country ?></p>
<center>
<input class="LeonardButton" type="button" id="calculate" value="Calculate">
</center>

After this, the user hits a button to send this data along with the data they fill out on page 2, to an javascript page for processing.

The javascript page attempts to take the variables by id and then alert them back to the user on the first page for testing purposes.

const $ = selector => document.querySelector(selector);

const focusAndSelect = selector => {
    const elem = $(selector);
    elem.focus();
    elem.select();  
};

const processEntries = () => 
{
    let name = document.getElementById("Name").value;
    let email = document.getElementById("Email").value;
    let company = document.getElementById("Company").value;
    let phone = document.getElementById("Phone").value;
    let city = document.getElementById("City").value;
    let state = document.getElementById("State").value;
    let country = document.getElementById("Country").value;
    
    alert("Test Alert");
    alert(name);
    alert(email);
    alert(company);
    alert(phone);
    alert(city);
    alert(state);
    alert(country); 
};

document.addEventListener("DOMContentLoaded", () => {
    $("#calculate").addEventListener("click", processEntries);
});

When I click the calculate button and run the javascript, it alerts me back, "test alert", then, "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined".

enter image description here I know that the php variables were set and defined on this page because I have them physically show up on top. enter image description here

Edit #1: I attempted to use Dzejkob's solution, but the variables are still coming in as undefined. Here is the code I modified and the results in a screenshot.

So I attempted the following code:

   let name = document.getElementById("Name").innerText;
    let email = document.getElementById("Email").innerText;
    let company = document.getElementById("Company").innerText;
    let phone = document.getElementById("Phone").innerText;
    let city = document.getElementById("City").innerText;
    let state = document.getElementById("State").innerText;
    let country = document.getElementById("Country").innerText;

and

    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;

But the result is the same:

Variables Are Undefined In Alerts Still

Edit #2: The answer suggested by Dzejkob worked. Apparently, I was pulling the wrong file in the cache. Ivar recommended that I check in the console and I did and then I saw that it was giving me old code.

Thank you both for your help!

  • 2
    Its just a typo, paragraphs don't have values, so use `innerText` instead – imvain2 Feb 26 '21 at 22:01
  • Does this answer your question? [Difference between innerText, innerHTML, and childNodes\[\].value?](https://stackoverflow.com/questions/19030742/difference-between-innertext-innerhtml-and-childnodes-value) – Ivar Feb 26 '21 at 22:17
  • @Ivar nope, I ran the test with both .innerText and with ,InnerHTML and they are both coming back as undefined. –  Feb 26 '21 at 22:28
  • 1
    @LeonardTreman [They shouldn't](https://jsfiddle.net/rky72fdc/). Caching maybe? – Ivar Feb 26 '21 at 22:32
  • @Ivar I thought the same thing, so I completely turned caching off for the staging site, I did one last server cache wipe, then I went incognito on google chrome and disabled the browser cache and had received these results. –  Feb 26 '21 at 22:37
  • 1
    @LeonardTreman Can you inspect the source in your browser and see if it indeed uses `.innerText`/`.innerHTML`? Maybe you are not changing the source you think you change? Like the good old "Making a change in the development environment, but refreshing production environment expecting to see the change" we all run in to from time to time? – Ivar Feb 26 '21 at 22:42
  • 1
    @Ivar I just tested the source and you were right. It was pulling old source code. Once I refreshed it it pulled the correct code successfully. –  Feb 26 '21 at 22:52

1 Answers1

3

You need to use document.getElementById("Name").innerHTML or .innerText instead of .value which is property of text field not paragraphs.

Dzejkob
  • 98
  • 6
  • I attempted both sets of these and both came back as undefined. let name = document.getElementById("Name").innerText; let email = document.getElementById("Email").innerText; let company = document.getElementById("Company").innerText; let phone = document.getElementById("Phone").innerText; let city = document.getElementById("City").innerText; let state = document.getElementById("State").innerText; let country = document.getElementById("Country").innerText; –  Feb 26 '21 at 22:22
  • 1
    Thats weird. Maybe you got more elements with the same id somewhere. Better to use console.log() and try to console.log(document.getElementById("Country")) whole element - than in console (ctrl+shift+i) at "console" tab you may see which element was found. – Dzejkob Feb 26 '21 at 22:33
  • I just tried the console and it's coming back error free in google chrome. I don't use these same elements anywhere. I will try using that console.log now –  Feb 26 '21 at 22:36