A very simple thing for someone who has ever done anything with java.

Discussion in 'JavaScript' started by x0x, Aug 22, 2008.

  1. #1
    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.
     
    x0x, Aug 22, 2008 IP
  2. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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;
     
    Dondon2d, Aug 22, 2008 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    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.
     
    x0x, Aug 22, 2008 IP
  4. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well your code needs to be rewritten to fulfill your needs.
     
    Dondon2d, Aug 22, 2008 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    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.
     
    x0x, Aug 22, 2008 IP
  6. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    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
     
    x0x, Aug 22, 2008 IP
  7. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Show me the page where you got the code.
     
    Dondon2d, Aug 22, 2008 IP
  8. jack_ss

    jack_ss Guest

    Messages:
    94
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    jack_ss, Aug 22, 2008 IP
  9. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #9
    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
     
    x0x, Aug 23, 2008 IP