Here is my javascript. The function of it is that there are 2 text boxes where users can write numbers. Gun is worth 5000, so they write 2 in gun text box I need the javascript to print out 10000. The code is perfect and I know that it works but how do I print it out? I have never dealed with javascript before, I just found this code. Oh yes, the printed amount must be with commas and can only be a number, never negative. <script language="JavaScript" type="text/JavaScript"> <!-- function addCommas(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } function characterCount(x) { var messageField = document.getElementById(x); // document.getElementById('charsBox').innerHTML = addCommas(parseInt(document.getElementById('charsBox').innerHTML) + (parseInt(messageField.value) * 2); var gun = document.getElementById('gun').value; var car = document.getElementById('car').value; if(! gun) gun = 0; if(! car) car = 0; var total = gun * 5000 + car * 2500; if(total <= x) { document.getElementById('charsBox').style.color = "#FF9900"; document.getElementById('charsBox').firstChild.nodeValue = addCommas(total); } else { document.getElementById('charsBox').style.color = "#990000"; document.getElementById('charsBox').firstChild.nodeValue = addCommas(total); } } //--> </script> PHP: How do I print the result? Any help is greatly appreciated.
It depends, if you are planning to just 'echo' it, you can use document.write, if you're planning to put in a text field then you can use document.formname.fieldname.value = total;
No not text field. It's like a card system. Users type the amount of stuff they want and it calculates the stuff together and prints it out. What is exactly that I have to put there? With the brackets and all. I'm a serious noob lol I tried this <script>document.write.value = total;</script> PHP: and no result. Also if there is nothing it should display plain zero. From the original code that I adopted I found this <label id="charsBox"><0</label> PHP: but I doubt that does anything. In fact, it didn't.
How come? I took it from another site and it worked fine there, just the print out part isn't showing up in the source for some reason, or I'm doing something wrong. gun car are the field names by the way.
It's <label id="charsBox"><0</label> for sure, I tested it. For some reason it doesnt work on my own page... Ok it's probably something to do with where I put the javascript. Where should I put it to make it work? right now I pasted it inside some html stuff
Your code works perfectly fine. You just need to run your function. First, I'd probably put your functions in the <head></head> or an included JS file. Second, and here's your problem, your code needs to be run by something. Probably wherever the "x" value is set (notice characterCount(x), "x" needs to be an argument). Maybe your need something like this: <input name="x" type="text" value="0" onkeyup="characterCount(this.value);"> Code (markup): To make this function work you will also need 2 input fields with "gun" and "car" ids.
Thank you, I think I can take it from here, although I have one more question. How do I put a php variable in here <input name="x" type="text" value="0" onkeyup="characterCount(this.value);"> "this.value" must be a php variable this didn't work for some reason: onKeyUp="characterCount(<? echo ($variable[0]); ?>);"> HTML: Thanks again. edit: it was my bad, the variable was called in the bottom of the page