Jump to content

Passing javascript variables


eva21

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="">

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.