eva21 Posted February 3, 2009 Share Posted February 3, 2009 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>'); } } Quote Link to comment https://forums.phpfreaks.com/topic/143629-passing-javascript-variables/ Share on other sites More sharing options...
KevinM1 Posted February 3, 2009 Share Posted February 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143629-passing-javascript-variables/#findComment-753620 Share on other sites More sharing options...
eva21 Posted February 3, 2009 Author Share Posted February 3, 2009 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/143629-passing-javascript-variables/#findComment-753627 Share on other sites More sharing options...
KevinM1 Posted February 3, 2009 Share Posted February 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143629-passing-javascript-variables/#findComment-753636 Share on other sites More sharing options...
eva21 Posted February 3, 2009 Author Share Posted February 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143629-passing-javascript-variables/#findComment-753641 Share on other sites More sharing options...
KevinM1 Posted February 3, 2009 Share Posted February 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143629-passing-javascript-variables/#findComment-753712 Share on other sites More sharing options...
eva21 Posted February 3, 2009 Author Share Posted February 3, 2009 <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=""> Quote Link to comment https://forums.phpfreaks.com/topic/143629-passing-javascript-variables/#findComment-753715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.