Jump to content

Recommended Posts

I have a javascript function that i need to pass variables so i can call it in php. What it does now is gives me the error: document.getElementById(...); is null or not an object. The thing is total1 and total2 are both global javascript variables. What happens now is it gives me the error, and than it shows the link, and i press it and i see that in the URL it has the correct values passing but nothing shows up in my php.

 

Here is the javascript function:

 

function checkBothTotals()

{

if(toatl1 > 0 && toatl2 > 0)

{

document.write('<a href="php_page.php?toatl1='+toatl1+'&total2='" +total2+ '" Next Page!</a>');

}

}

Link to comment
https://forums.phpfreaks.com/topic/143629-passing-javascript-variables/
Share on other sites

I have a javascript function that i need to pass variables so i can call it in php. What it does now is gives me the error: document.getElementById(...); is null or not an object. The thing is total1 and total2 are both global javascript variables. What happens now is it gives me the error, and than it shows the link, and i press it and i see that in the URL it has the correct values passing but nothing shows up in my php.

 

Here is the javascript function:

 

function checkBothTotals()

{

if(toatl1 > 0 && toatl2 > 0)

{

document.write('<a href="php_page.php?toatl1='+toatl1+'&total2='" +total2+ '" Next Page!</a>');

}

}

 

It's probably do to you not waiting for the HTML to be finished rendering before your script tries to get the necessary element references.  But, I can't tell for sure until you show more code.  Specifically, how/where the checkBothTotals() function is called.

function doCalc()

{

if(num1 && num2)

{

var total = 0;

total = parseFloat(num1) * parseFloat(num2);

total1 = total;

total1 = total.toFixed(2);

document.getElementById("total1").value = "$" + total1;

checkBothTotals();

}

}

function doCalc2()

{

if(num3 && num4)

{

var total = 0;

total = parseFloat(num3) * parseFloat(num4);

 

total1 = total.toFixed(2);

document.getElementById("total2").value = "$" + total2;

checkBothTotals();

}

}

function doCalc()

{

if(num1 && num2)

{

var total = 0;

total = parseFloat(num1) * parseFloat(num2);

total1 = total;

total1 = total.toFixed(2);

document.getElementById("total1").value = "$" + total1;

checkBothTotals();

}

}

function doCalc2()

{

if(num3 && num4)

{

var total = 0;

total = parseFloat(num3) * parseFloat(num4);

 

total1 = total.toFixed(2);

document.getElementById("total2").value = "$" + total2;

checkBothTotals();

}

}

 

Sorry, but I need to see more.  Where do num's 1, 2, 3, and 4 come from?  Where are these functions located?  Are they merely between <script> tags?  Is there any more context you can give me?

 

EDIT: You should probably be doing something like:

 

document.getElementById('total2').innerHTML = "$" + total2;

 

Rather than trying to use the value attribute.

num1, num2, num3 and num4 are textboxes waiting for user input. I have an onblur function that calls doCalc and doCalc2 right as soon as there is a valid number in there.

 

Still vague....

 

I need to see code in order to help you.  There could be several issues at play here, but without seeing exactly how you've written things, I can't diagnose the problem(s) nor suggest a cure.

<input type=text size="9" MAXLENGTH="9" id="num1" onblur="doCalc()" name="num1" value="">

<input type=text size="9" MAXLENGTH="9" id="num2" onblur="doCalc()" name="num1" value="">

<input type=text size="9" MAXLENGTH="9" id="total1" name="total1" value="">

 

<input type=text size="9" MAXLENGTH="9" id="num3" onblur="doCalc2()" name="num3" value="">

<input type=text size="9" MAXLENGTH="9" id="num4" onblur="doCalc2()" name="num4" value="">

<input type=text size="9" MAXLENGTH="9" id="total2" name="total2" value="">

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.